Reputation:
I have a program with two very large lists, one with 3^12 terms and the other with 5000 terms, and I want to find the intersection of them (items that are the same in both lists).
I have already tried using sets and loops(see below).
I have tried (in Python 3)
[i for i in joinedCombs if i in dictionary]
and
endResult = list(set(joinedCombs)&set(dictionary))
I get a time error for the first line of code and a memory error for the second line of code. What could I do to balance out speed and time? Please leave an answer, not a comment
Upvotes: 0
Views: 140
Reputation: 532448
Use len
to determine which list is the shorter one, and create a set from that:
[i for i in joinedCombs if i in dictionary]
if len(joinedCombs) < len(dictionary):
s1 = set(dictionary)
itr = joinedCombs
else:
s1 = set(joinedCombs)
itr = dictionary
new = [i for i in itr if i in s1]
Upvotes: 1
Reputation: 1510
You can try this line of code :
endResult = list(set(joinedCombs).intersection(dictionary))
Upvotes: 0