查找列表列表中的常用元素

Fla*_*Dra 4 python list set

我有一个名为wordlist的单词列表,如下所示:

[['dog', 'cat', 'sheep', 'rabbit', 'kiss', 'time'], ['cow', 'pig', 'bomb', 'cat', 'sheep', 'cake', 'boy', 'new']]
Run Code Online (Sandbox Code Playgroud)

我想在所有子列表中找到共同的元素.因此,我希望输出的上述列表应为:

['cat', 'sheep']
Run Code Online (Sandbox Code Playgroud)

为了实现这一点,我使用以下代码创建了集合:

sets = set(tuple(row) for row in wordlist)
Run Code Online (Sandbox Code Playgroud)

该集看起来像这样:

{('cow', 'pig', 'bomb', 'cat', 'sheep', 'cake', 'boy', 'new'), ('dog', 'cat', 'sheep', 'rabbit', 'kiss', 'time')}
Run Code Online (Sandbox Code Playgroud)

每个列表可以有任意数量的单词,并且可以有任意数量的列表.所以我最终会得到任何数字的不均匀集.我知道我可以使用交集方法比较两个集合,但是如何比较多个集合以仅返回常见项目?

Moi*_*dri 13

您使用set不当.您可以像以下一样使用它:

my_list = [['dog', 'cat', 'sheep', 'rabbit', 'kiss', 'time'], ['cow', 'pig', 'bomb', 'cat', 'sheep', 'cake', 'boy', 'new']]

# convert list of list to list of sets
my_sets = map(set, my_list)

# perform intersection on each set present in list
common_items = set.intersection(*my_sets)
Run Code Online (Sandbox Code Playgroud)

这可以写成一行:

common_items = set.intersection(*map(set, my_list))
Run Code Online (Sandbox Code Playgroud)

持有的价值common_items将是:

{'sheep', 'cat'}
Run Code Online (Sandbox Code Playgroud)

以下是采用性能效率稍高的方法提供相同结果的解决方案:

#                              v no need to type-cast sub-lists to `set` here
set(my_list[0]).intersection(*my_list[1:])

# OR,
# set(my_list[0]).intersection(*my_list)
# as intersection of set with itself returns the same set
Run Code Online (Sandbox Code Playgroud)

由于set.intersection接受所有迭代,因此无需对要设置的所有子列表进行类型转换.