2

如果 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
4

2 回答 2

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')]
于 2013-02-27T08:46:41.363 回答
0

基本上,您不需要为 list1 中的元素设置集合。如果检查列表 1 中的每个元组,您所需要的只是它们的元素是否在列表 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 )]

由于您没有提供有关代码失败情况的详细信息,因此无法检查这是否适用于您失败的示例。

于 2013-02-27T11:09:31.633 回答