将两个词典与列表类型值进行比较

时间:2022-02-12 04:39:24

I'm trying to compare two dictionaries. My approach is to turn them into two separate lists of tuples and to then use the set module. Here is an illustration:

我想比较两个词典。我的方法是将它们变成两个单独的元组列表,然后使用set模块。这是一个例子:

dict = {'red':[1,2,3],'blue':[2,3,4],'green':[3,4,5]}

dict1 = {'green':[3,4,5],'yellow':[2,3,4],'red':[5,2,6]}

intersection = set(set(dict.items()) & set(dict1.items()))

apparently, this is comparing two lists of tuples and python doesn't like that. I get a TypeError: 'list' is unhashable error (or similar wording).

显然,这是比较两个元组列表而python不喜欢这样。我得到一个TypeError:'list'是不可用的错误(或类似的措辞)。

I would like intersection to contain [('green',[3,4,5])]. Any ideas?

我希望交集包含[('green',[3,4,5])]。有任何想法吗?

2 个解决方案

#1


6  

shared_keyvals = dict( (key, dict1[key])
                       for key in (set(dict1) & set(dict2))
                       if dict1[key] == dict2[key]
                     )

You can even make this into a function:

你甚至可以把它变成一个函数:

def shared_keyvals(dict1, dict2):
    return dict( (key, dict1[key])
                 for key in (set(dict1) & set(dict2))
                 if dict1[key] == dict2[key]
               )

Obviously, if you prefer to not have the output in dictionary form, you can just remove the dict() call and replace with with list comprehension brackets ([]) instead.

显然,如果您不希望以字典形式输出,则可以删除dict()调用并替换为list comprehension bracket([])。

#2


2  

Lists are mutable and, thus, not hashable. A set can be built only out of hashable items. Since values in the two dicts above are lists, sets cannot be built out of them. However, if one were to change the type of values from lists to tuples (which are immutable), one could build sets and perform set operations.

列表是可变的,因此不可清除。只能用可拆卸物品建造一套。由于上面两个dicts中的值是列表,因此无法构建集合。但是,如果要将值的类型从列表更改为元组(这是不可变的),则可以构建集合并执行集合操作。

>>> dict1 = {'blue': (2, 3, 4), 'green': (3, 4, 5), 'red': (1, 2, 3)}
>>> dict2 = {'green': (3, 4, 5), 'yellow': (2, 3, 4), 'red': (5, 2, 6)}
>>> list(set(dict1.items()) & set(dict2.items()))
[('green', (3, 4, 5))]

#1


6  

shared_keyvals = dict( (key, dict1[key])
                       for key in (set(dict1) & set(dict2))
                       if dict1[key] == dict2[key]
                     )

You can even make this into a function:

你甚至可以把它变成一个函数:

def shared_keyvals(dict1, dict2):
    return dict( (key, dict1[key])
                 for key in (set(dict1) & set(dict2))
                 if dict1[key] == dict2[key]
               )

Obviously, if you prefer to not have the output in dictionary form, you can just remove the dict() call and replace with with list comprehension brackets ([]) instead.

显然,如果您不希望以字典形式输出,则可以删除dict()调用并替换为list comprehension bracket([])。

#2


2  

Lists are mutable and, thus, not hashable. A set can be built only out of hashable items. Since values in the two dicts above are lists, sets cannot be built out of them. However, if one were to change the type of values from lists to tuples (which are immutable), one could build sets and perform set operations.

列表是可变的,因此不可清除。只能用可拆卸物品建造一套。由于上面两个dicts中的值是列表,因此无法构建集合。但是,如果要将值的类型从列表更改为元组(这是不可变的),则可以构建集合并执行集合操作。

>>> dict1 = {'blue': (2, 3, 4), 'green': (3, 4, 5), 'red': (1, 2, 3)}
>>> dict2 = {'green': (3, 4, 5), 'yellow': (2, 3, 4), 'red': (5, 2, 6)}
>>> list(set(dict1.items()) & set(dict2.items()))
[('green', (3, 4, 5))]