I have many tuples (e.g. (0, 1)
) in two pretty long lists list_0
and list_1
of size ~40k elements. I need to count the tuples of list_0
also in list_1
.
The following statement with list comprehension takes ~ 1 min and I need to do this multiple times, so I am looking for something more efficient :
len([element for element in list_0 if element in list_1])
What could be more efficient ?
To reproduce :
elements_0 = 200*[0]+50*[1]+150*[2]
elements_1 = 100*[1]+150*[2]+150*[1]
df = pd.DataFrame(data=[list(elements_0), list(elements_1)]).T
list_0 = [item for sublist in df.groupby(0)[0].apply(lambda x: list(combinations(x.index, 2))) for item in sublist]
list_1 = [item for sublist in df.groupby(1)[1].apply(lambda x: list(combinations(x.index, 2))) for item in sublist]
print(len([pair for pair in list_0 if pair in list_1])) # Long