I have two large lists of points in 2D and I want to find their common sublists, if they have some. Both of the lists are quite large and efficiency is an issue.
t1 = [[3, 41], [5, 82], [10, 31], [11, 34], [14, 54]]
t2 = [[161, 160], [169, 260], [187, 540], [192, 10], [205, 23]]
I tried itertools like below, but I get "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()".
for i in itertools.chain.from_iterable(t1):
if i in t2:
print "yes",i
I tried the first answer from here too, but I get 'numpy.int64' object is not iterable. Also, I think this simple code would work, but it takes so much time:
intersection = [i for i in t1 if i in t2]
Any advice? Thanks.
Lists are not hashable so we need to convert the inner list to tuple then we can use set intersection to find common element
Output
Since we are making the list into sets we are not accounting for repetitions. consider the following inputs
We have two [3,41] in both the lists but the previous python program will output only one [3,41] in the output. The following program will handle duplicate entries by counting them initially and repeating them after.
Output