Find lists in list of lists with the most instances from another list

I have a list:

a = [10,9,1,2,3,4] 

And a list of lists:

b = [[1,2,3],[6,9,10],[4,5,7,8]]

I want to get:

c = [[1,2,3],[6,9,10]]

Where c is the list of top 2 lists in b with the most instances in a.
These can be large, so performance could be an issue.

Not sure if this makes a differences, but each element is present only once in a and the entirety of b and is an integer.

>Solution :

The heapq module has a nlargest function that will efficiently get you the top n values from a list without sorting the whole thing. Use a set to get the intersection of values in a with each sub list:

from heapq import nlargest


a = [10,9,1,2,3,4] 


b = [[1,2,3],[6,9,10],[4,5,7,8]]

aSet = set(a)
c    = nlargest(2,b,key=lambda n:len(aSet.intersection(n)))

print(c)

[[1, 2, 3], [6, 9, 10]]

Leave a Reply