If order is not important and you don't need to worry about duplicates then you can use set intersection:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]
Answer from Mark Byers on Stack OverflowIf order is not important and you don't need to worry about duplicates then you can use set intersection:
>>> a = [1,2,3,4,5]
>>> b = [1,3,5,6]
>>> list(set(a) & set(b))
[1, 3, 5]
Using list comprehensions is a pretty obvious one for me. Not sure about performance, but at least things stay lists.
[x for x in a if x in b]
Or "all the x values that are in A, if the X value is in B".
Videos
Hi all,
I've got an assignment I'm working on for a bioinformatics course in which we've converted several text files into lists, and want to find each instance of any two of these lists having an item in common.
For example, let's say we've got Jenny, Martha, Bertha, and Ingrid. Each of these fine ladies has a grocery list, and what we want to do is compare each of these ladies' grocery lists with one another and return any items that are in common. As such...
martha_list = ['Eggs','Sugar','Milk']
bertha_list = ['Bread','Cereal','Popcorn']
ingrid_list = ['Flour','Milk']
jenny_list = ['Flour','Milk','Poptarts']
What I want to do is establish a for loop that compares every possible combination of grocery lists, and returns each example of two lists sharing a common item. Here's what I have:
groceries = [martha_list, bertha_list, ingrid_list, jenny_list]
for i in range(len(groceries)):
for j in range(i+1,len(groceries)):
comp = (groceries[i] & groceries[j])
## or: comp = groceries[i].intersection(groceries[j])
print(comp,"\n")
Both the "intersection" and "&" strategies return different errors: the former an AttributeError ('list' object has no attribute 'intersection'), and the latter a TypeError (unsupported operand type(s) for &: 'list' and 'list').
I tried to fix this by converting the list objects into sets, but a set(groceries) or set(groceries[i]) command returns another TypeError ('list' object is not callable).
I've tried googling the hell out of this and haven't been able to find a solution, so any help would be very appreciated. I know that my print command at the end won't return exactly what I want, but I'm having a hard time coming up with what I'll want to print without knowing how to set the loop up to begin with.
Does anyone know how i can use a merging algorithm that is efficient and reduces the number of comparison to a minimum? (a and b are sorted lists)
I thought about an skip pointer but i don't know how to implement it
dict_ratings = {}
a = sorted([f for f in inverted_index['american']])
b = sorted([f for f in inverted_index['dream']])
i, j = 0, 0
intersection = []
while i < len(a) and j < len(b):
if a[i] == b[j]:
intersection.append(a[i])
i += 1
j += 1
elif a[i] > b[j]:
j += 1
else:
i += 1
print(intersection)set.intersection(*map(set,d))
for 2.4, you can just define an intersection function.
def intersect(*d):
sets = iter(map(set, d))
result = sets.next()
for s in sets:
result = result.intersection(s)
return result
for newer versions of python:
the intersection method takes an arbitrary amount of arguments
result = set(d[0]).intersection(*d[1:])
alternatively, you can intersect the first set with itself to avoid slicing the list and making a copy:
result = set(d[0]).intersection(*d)
I'm not really sure which would be more efficient and have a feeling that it would depend on the size of the d[0] and the size of the list unless python has an inbuilt check for it like
if s1 is s2:
return s1
in the intersection method.
>>> d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]
>>> set(d[0]).intersection(*d)
set([3, 4])
>>> set(d[0]).intersection(*d[1:])
set([3, 4])
>>>