开发者

Comparing Python nested lists

开发者 https://www.devze.com 2022-12-15 17:28 出处:网络
I have two nested lists, each nested list containing two strings e.g.: list 1 [(\'EFG\', \'[3,4,5]\'), (\'DEF\', \'[2,3,4]\')] and list 2 [(\'DEF\', \'[2,3,4]\'), (\'FGH\', \'[4,5,6]\')]

I have two nested lists, each nested list containing two strings e.g.:

list 1 [('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')] and list 2 [('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')]

I would like to compare the two l开发者_JAVA百科ists and recover those nested lists which are identical with each other. In this case only ('DEF','[2,3,4]') would be returned. The lists could get long. Is there an efficient way to do this?


If the lists only contain string tuples, then the easiest way to do it is using sets intersection (&):

>>> set([('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')]) & set([('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')])
set([('DEF', '[2,3,4]')])


Using sets to do this is a very good implementation, but for optimal performance you should use only one set:

set1 = set([('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')])
matches = [x for x in [('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')] if x in set1]


Here's one approach. Convert your lists to sets and find their intersections.

>>> a,b = set([('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')]), set([('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')])
>>> print a.intersection(b)
set([('DEF', '[2,3,4]')])

I don't think it will degrade much with the length of your lists.


Just use set the builtin support for sets

def get_set(list1,list2):
    s = set(list1)
    s &= set(list2)
    return s

And Boom there's your union


intersection = [item for item in list1 if item in list2]

But only use it, if for some reason you have mutables in your lists and can’t convert to a set. Of course, it’s all depending on the size of your lists and the number of duplicates, but it could well be a factor of 10 times slower.


     python 3.2
     [i for i in list1 for v in list2 if i==v]
0

精彩评论

暂无评论...
验证码 换一张
取 消