Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading