Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python compare a list of lists efficiently

Tags:

python

list

set

I have a long list of long lists so efficiency is an issue for me. I wondered if there was a neater way of comparing a list of lists other than looping over a list within a loop of the same list (easier to see by example)

matchList=[]
myList = [ ('a',[1,2,3]), ('b', [2,3,4]), ('c', [3,4,5]), ('d', [4,5,6]) ]

tup_num=1
for tup in myList:
    for tup2 in myList[tup_num:]:
        id=str(tup[0])+':'+str(tup2[0])
        matches=set(tup[1]) & set(tup2[1])
        matchList.append((id,matches))
    tup_num+=1

print matchList

Output:

[('a:b', set([2, 3])), ('a:c', set([3])), ('a:d', set([])), ('b:c', set([3, 4])), ('b:d', set([4])), ('c:d', set([4, 5]))]

This works and doesn't repeat comparisons but I'm sure there must be a better way of doing it.

Cheers

like image 997
JoshuaBox Avatar asked Apr 12 '26 14:04

JoshuaBox


1 Answers

Using itertools.combinations:

>>> import itertools
>>> matchList = []
>>> myList = [('a',[1,2,3]), ('b', [2,3,4]), ('c', [3,4,5]), ('d', [6,7,8])]
>>> matchList = [
...     ('{}:{}'.format(key1, key2), set(lst1) & set(lst2))
...     for (key1, lst1), (key2, lst2) in itertools.combinations(myList, 2)
... ]
>>> matchList
[('a:b', set([2, 3])), ('a:c', set([3])), ('a:d', set([])), ('b:c', set([3, 4])), ('b:d', set([])), ('c:d', set([]))]
like image 145
falsetru Avatar answered Apr 15 '26 03:04

falsetru