在python中从两个元组列表中选择元组,如果列表中包含公共元素

时间:2013-02-27 08:41:46

标签: python list tuples set

如果list1中的元素存在或者在list2中是常见的,我想从list1的元组创建一个新的元组列表。

list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
         ('a', 'yellow'), ('yellow', 'submarine.')]

list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
         ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
         ('a', 'sea.')]

预期输出= [('live', 'in'), ('in', 'a'), ('a', 'yellow')]

我的代码如下:它适用于这种情况,但在某些情况下在大型数据集中失败。

All_elements_set1 = set([item for tuple in list1 for item in tuple])

All_elements_set2 = set([item for tuple in list2 for item in tuple])


common_set = All_elements_set1 & All_elements_set2

new_list = [(i,v) for i,v in list1 if i (in common_set and v in common_set)]

print new_list

2 个答案:

答案 0 :(得分:3)

In [39]: from itertools import chain

In [40]: list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
    ...:          ('a', 'yellow'), ('yellow', 'submarine.')]
    ...: 
    ...: list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
    ...:          ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
    ...:          ('a', 'sea.')]
    ...: 

In [41]: elems = set(chain.from_iterable(list2))

In [42]: [tup for tup in list1 if elems.issuperset(tup)]
Out[42]: [('live', 'in'), ('in', 'a'), ('a', 'yellow')]

答案 1 :(得分:0)

基本上,您不需要为list1中的元素创建一个集合。如果对list1中的每个元组检查它们的元素是否在列表2中的某个元组中,那么你只需要...

list1 = [('We', 'all'), ('all', 'live'), ('live', 'in'), ('in', 'a'),
         ('a', 'yellow'), ('yellow', 'submarine.')]

list2 = [('A', 'live'), ('live', 'yellow'), ('yellow', 'submarine'),
         ('submarine', 'lifeform'), ('lifeform', 'in'), ('in', 'a'),
         ('a', 'sea.')]

Elements_set2 = set([item for tuple in list2 for item in tuple])

print [(i,v) for i,v in list1 if (i in Elements_set2 and v in Elements_set2 )]

由于您没有详细说明代码失败的情况,因此无法检查此代码是否适用于您的失败示例。