>>> L1 = [2,3,4]
>>> L2 = [1,2]
>>> [i for i in L1 if i in L2]
[2]
>>> S1 = set(L1)
>>> S2 = set(L2)
>>> S1.intersection(S2)
set([2])
Both empty lists and empty sets are False, so you can use the value directly as a truth value.
Answer from Joe Koberg on Stack Overflow>>> L1 = [2,3,4]
>>> L2 = [1,2]
>>> [i for i in L1 if i in L2]
[2]
>>> S1 = set(L1)
>>> S2 = set(L2)
>>> S1.intersection(S2)
set([2])
Both empty lists and empty sets are False, so you can use the value directly as a truth value.
I was thinking of this slight variation on Tobias' solution:
>>> a = [1,2,3,4]
>>> b = [2,7]
>>> any(x in a for x in b)
True
I'm learning Haskell, and I'm stuck on the following problem:
I can do this:
'a' `elem` "Some String"
But I want to do the following:
['a'..'z'] `elem` "Some String"
This doesn't work. I want to check if all the elements of ['a'..'z'] are in the string, but I can't figure out how to express that other than like this:
let string = "Some String"
result = 'a' `elem` string &&
'b' `elem` string &&
...
'z' `elem` stringHow can I do that?
Operators like <= in Python are generally not overriden to mean something significantly different than "less than or equal to". It's unusual for the standard library does this--it smells like legacy API to me.
Use the equivalent and more clearly-named method, set.issubset. Note that you don't need to convert the argument to a set; it'll do that for you if needed.
set(['a', 'b']).issubset(['a', 'b', 'c'])
I would probably use set in the following manner :
set(l).issuperset(set(['a','b']))
or the other way round :
set(['a','b']).issubset(set(l))
I find it a bit more readable, but it may be over-kill. Sets are particularly useful to compute union/intersection/differences between collections, but it may not be the best option in this situation ...
Here's what I've got so far:
list1 = ["a", ["b", "c"], ["d", "e"]] list2 = ["b", "c", "f"] check = any(item in list1 for item in list2) print(check)
How would I check whether or not list2 contains an element of list1, including one of the nested lists? It returns True if I put "a" in list2, but gives False as it is right now. I'm pretty sure this is because it's checking single items, but I don't know how to make it work.
Any help would be greatly appreciated!
Hello all,
I am trying to create a list (list3) of all items that are exist in one list (list1), but do not exist in another list (list2). Any help would be greatly appreciated!
I am using pandas / data frames to create two lists each made of the items in a column in different excel files and I want to identify the items in list1 that are not found in list2.
Basically I have:
Df1 = pd.read_excel(file1)
Df2 = pd.read_excel(file2)
List1 = df1['Column Name']
List2 = df2['Column Name']
List3 = [x for x in List1 if x not in List2]
This is basically just recreating List1 and not omitting the entries that exist in List2.
Thank you all in advance! Please let me know if you need more info
When number of occurrences doesn't matter, you can still use the subset functionality, by creating a set on the fly:
>>> list1 = ['a', 'c', 'c']
>>> list2 = ['x', 'b', 'a', 'x', 'c', 'y', 'c']
>>> set(list1).issubset(list2)
True
If you need to check if each element shows up at least as many times in the second list as in the first list, you can make use of the Counter type and define your own subset relation:
>>> from collections import Counter
>>> def counterSubset(list1, list2):
c1, c2 = Counter(list1), Counter(list2)
for k, n in c1.items():
if n > c2[k]:
return False
return True
>>> counterSubset(list1, list2)
True
>>> counterSubset(list1 + ['a'], list2)
False
>>> counterSubset(list1 + ['z'], list2)
False
If you already have counters (which might be a useful alternative to store your data anyway), you can also just write this as a single line:
>>> all(n <= c2[k] for k, n in c1.items())
True
Be aware of the following:
>>>listA = ['a', 'a', 'b','b','b','c']
>>>listB = ['b', 'a','a','b','c','d']
>>>all(item in listB for item in listA)
True
If you read the "all" line as you would in English, This is not wrong but can be misleading, as listA has a third 'b' but listB does not.
This also has the same issue:
def list1InList2(list1, list2):
for item in list1:
if item not in list2:
return False
return True
Just a note. The following does not work:
>>>tupA = (1,2,3,4,5,6,7,8,9)
>>>tupB = (1,2,3,4,5,6,6,7,8,9)
>>>set(tupA) < set(TupB)
False
If you convert the tuples to lists it still does not work. I don't know why strings work but ints do not.
Works but has same issue of not keeping count of element occurances:
>>>set(tupA).issubset(set(tupB))
True
Using sets is not a comprehensive solution for multi-occurrance element matching.
But here is a one-liner solution/adaption to shantanoo's answer without try/except:
all(True if sequenceA.count(item) <= sequenceB.count(item) else False for item in sequenceA)
A builtin function wrapping a list comprehension using a ternary conditional operator. Python is awesome! Note that the "<=" should not be "==".
With this solution sequence A and B can be type tuple and list and other "sequences" with "count" methods. The elements in both sequences can be most types. I would not use this with dicts as it is now, hence the use "sequence" instead of "iterable".
In C, there are two text related data types: char and strings. A string is a pointer to an array of chars, that by convention is terminated with a NUL character ('\0'). A char is a single byte representing an ASCII character.
You create a string literal by compiling a series of characters contained between opening and closing double quotes: "this is a string literal"
On the other hand, you create a char literal by compiling a single character expression - possibly with a backslash escape - between a pair of single quotes: 't', 'h', 'e', 's', 'e', ' ', 'a', 'r', 'e', ' ', 'c', 'h', 'a', 'r', ' ', 'l', 'i', 't', 'e', 'r', 'a', 'l', 's', '\n'
After the advent of code pages and multi-byte character encodings, C added (grudging) support for "wide chars" (that is, more than 8 bits used to store a single element) and for "multi-byte chars" (that is, character encodings that used 8-bit values, but required more than one to encode a single glyph).
The compiler is seeing your line initializing the array:
char a[]={'a','5ab','na','8s'};
And is assuming that '5ab' and 'na' and '8s' are an attempt, by you, to specify a wide or multi-byte character literal. It knows that characters (not strings) are enclosed in single quotes. It knows that you're using type char. So these can't be strings. But they aren't multi-byte characters, either.
In reality, I think you meant for them to be strings. You're probably coming from python, or bash, or perl or some other language where single quotes can delimit strings. But that's not C.
To create a list of strings, you need to know that strings are of type char * (or const char *). You then want to create an array of them - that's your list:
const char *list_of_str[] = {
"hello",
"my name",
"is George"
};
You'll notice that argv is a list of strings. ;-)
You cannot have '5ab' in your a[] array. An array of characters can only hold single characters, such as 'a', '5', or '\n'.
Also, I'm not sure if it's a good idea to to i < (argc) in your for loop. argc is the number of elements that you pass in when your C program is ran. Try i < 4 and make sure the i in your for loop starts at 0.