dictonary值,所以說,我有我的字典Python 3的轉換爲一組
In [80]: dict_of_lists
Out[80]:
{'Marxes': ['Groucho', 'Chico', 'Harpo'],
'Pythons': ['Chapman', 'Cleese', 'Gilliam'],
'Stooges': ['Larry', 'Curly', 'Moe']}
,我意識到,以後我會想對待值集。如何將字典從值(列表)轉換爲值(集)結構?
這是whati試過。
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
請出示你想看到的結果 – 2014-10-11 11:33:59
您實際上是創建值列表什麼樣的一套作爲集合,而不是字典。這是你想要的嗎? – 2014-10-11 11:36:21