dict中的交叉列表(超过两个)

时间:2023-02-03 18:02:02

I have a dict, of varying length. Each entry has a name and a list as so:

我有一个不同长度的词典。每个条目都有一个名称和一个列表,如下所示:

somedict = {'Name': [1, 2, 3], 'Name2': [], 'Name3': [2,3] }

How do I get the intersection for the following list? I need to do it dynamically, I don't know how long the dict will be.

如何获得以下列表的交集?我需要动态地做,我不知道dict会有多长。

For the above list, the intersection would be empty, I know. But for

对于上面的列表,我知道交叉点是空的。但对于

somedict = {'Name': [1, 2, 3], 'Name3': [2,3] }

It should return

它应该回来

[2, 3]

2 个解决方案

#1


11  

Normally, intersection is a set operation. So, you might want to convert the values of the dictionary to sets and then run intersection, like this

通常,交集是一种设定操作。因此,您可能希望将字典的值转换为集合,然后像这样运行交集

>>> set.intersection(*(set(values) for values in data.values()))
{2, 3}

If you want the result to be a list, just convert the resulting set to a list, like this

如果您希望结果为列表,只需将结果集转换为列表,如下所示

>>> list(set.intersection(*(set(values) for values in data.values())))
[2, 3]

Here, the expression, *(set(values) for values in data.values()) creates a generator, which yields each and every value of the dictionary item converted to a set and the generator is unpacked to the set.intersection function.

这里,表达式*(data.values()中值的set(values))创建一个生成器,它生成转换为集合的字典项的每个值,并将生成器解压缩到set.intersection函数。

#2


1  

Provide another way using reduce.

提供另一种使用reduce的方法。

reduce(lambda x,y: set(x) & set(y), the_list)

the way it behave is like(e.g the_list = [[1, 2, 3], [], [2,3]]):

它的行为方式就像(例如the_list = [[1,2,3],[],[2,3]]):

set([1,2,3]) & set([]) => tmp_result
set(tmp_result) & set([2,3]) => final_result

so the solution will be:

所以解决方案是:

>>> dict_one = {'Name': [1, 2, 3], 'Name2': [], 'Name3': [2, 3]}
>>> reduce(lambda x,y: set(x) & set(y), dict_one.values())
set([])
>>> dict_two = {'Name': [1, 2, 3], 'Name3': [2, 3]}
set([2, 3])
>>> list(dict_two)
[2, 3]

#1


11  

Normally, intersection is a set operation. So, you might want to convert the values of the dictionary to sets and then run intersection, like this

通常,交集是一种设定操作。因此,您可能希望将字典的值转换为集合,然后像这样运行交集

>>> set.intersection(*(set(values) for values in data.values()))
{2, 3}

If you want the result to be a list, just convert the resulting set to a list, like this

如果您希望结果为列表,只需将结果集转换为列表,如下所示

>>> list(set.intersection(*(set(values) for values in data.values())))
[2, 3]

Here, the expression, *(set(values) for values in data.values()) creates a generator, which yields each and every value of the dictionary item converted to a set and the generator is unpacked to the set.intersection function.

这里,表达式*(data.values()中值的set(values))创建一个生成器,它生成转换为集合的字典项的每个值,并将生成器解压缩到set.intersection函数。

#2


1  

Provide another way using reduce.

提供另一种使用reduce的方法。

reduce(lambda x,y: set(x) & set(y), the_list)

the way it behave is like(e.g the_list = [[1, 2, 3], [], [2,3]]):

它的行为方式就像(例如the_list = [[1,2,3],[],[2,3]]):

set([1,2,3]) & set([]) => tmp_result
set(tmp_result) & set([2,3]) => final_result

so the solution will be:

所以解决方案是:

>>> dict_one = {'Name': [1, 2, 3], 'Name2': [], 'Name3': [2, 3]}
>>> reduce(lambda x,y: set(x) & set(y), dict_one.values())
set([])
>>> dict_two = {'Name': [1, 2, 3], 'Name3': [2, 3]}
set([2, 3])
>>> list(dict_two)
[2, 3]