【发布时间】:2014-10-11 11:29:17
【问题描述】:
所以说我有我的字典
In [80]: dict_of_lists
Out[80]:
{'Marxes': ['Groucho', 'Chico', 'Harpo'],
'Pythons': ['Chapman', 'Cleese', 'Gilliam'],
'Stooges': ['Larry', 'Curly', 'Moe']}
我意识到稍后我会希望将这些值视为集合。如何将字典从值(列表)转换为值(集)结构?
这是我尝试过的。
In [84]: new_dict = [set(dict_of_lists.values()) for values in dict_of_lists.keys()]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-84-f49792cd81ac> in <module>()
----> 1 new_dict = [set(dict_of_lists.values()) for values in dict_of_lists.keys()]
<ipython-input-84-f49792cd81ac> in <listcomp>(.0)
----> 1 new_dict = [set(dict_of_lists.values()) for values in dict_of_lists.keys()]
TypeError: unhashable type: 'list'
还有这种相当丑陋的努力。
In [83]: for list(dict_of_lists.keys()) in dict_of_lists:
....: set list(dict
dict dict_of_lists
....: set(list(dict_of_lists.values()))
....:
File "<ipython-input-83-08e0645abb2f>", line 1
for list(dict_of_lists.keys()) in dict_of_lists:
^
SyntaxError: can't assign to function call
【问题讨论】:
-
请显示您希望看到什么样的结果集
-
您实际上是在创建一个值列表作为集合,而不是字典。这是你想要的吗?
标签: python python-3.x dictionary