Python 错误:不可散列的类型:'list'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16803393/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python error: unhashable type: 'list'
提问by stgeorge
I started learning Python few weeks ago (with no previous knowledge of it nor programming). I want to create a definition which will for given dictionary as an argument return a tuple consisted of two lists - one where there are only keys of dictionary, and the other where there are only values of the given dictionary. Basically the code will look like this:
几周前我开始学习 Python(之前既不了解 Python 也不编程)。我想创建一个定义,它将给定的字典作为参数返回一个由两个列表组成的元组 - 一个只有字典的键,另一个只有给定字典的值。基本上代码看起来像这样:
"""Iterate over the dictionary named letters, and populate the two lists so that 
keys contains all the keys of the dictionary, and values contains 
all the corresponding values of the dictionary. Return this as a tuple in the end."""
def run(dict):
    keys = []
    values = []
    for key in dict.keys():
        keys.append(key)
    for value in dict.values():
        values.append(value)
    return (keys, values)
print run({"a": 1, "b": 2, "c": 3, "d": 4})
This code worked perfectly (it's not my solution though). But what if I do not want to use the .keys()and .values()methods? In that case, I tried using something like this, but I got "unhashable type: 'list'" error message:
这段代码工作得很好(虽然这不是我的解决方案)。但是如果我不想使用.keys()和.values()方法怎么办?在那种情况下,我尝试使用这样的东西,但我收到了“unhashable type: 'list'”错误消息:
def run(dict):
    keys = []
    values = []
    for key in dict:
        keys.append(key)
        values.append(dict[keys])
    return (keys, values)
print run({"a": 1, "b": 2, "c": 3, "d": 4})
What seems to be the problem?
似乎是什么问题?
采纳答案by Martijn Pieters
You are trying to use the whole keyslist as a key:
您正在尝试使用整个keys列表作为键:
values.append(dict[keys])
Perhaps you meant to use dict[key]instead? A listis a mutable type, and cannot be used as a key in a dictionary (it could change in-place making the key no longer locatable in the internal hash table of the dictionary).
也许您打算dict[key]改用?Alist是可变类型,不能用作字典中的键(它可以就地更改,使键不再可在字典的内部哈希表中定位)。
Alternatively, loop over the .items()sequence:
或者,循环遍历.items()序列:
for key, value in dct.items():
    keys.append(key)
    values.append(value)
Please don't use dictas a variable name; you are shadowing the built-in type by doing that.
请不要dict用作变量名;你这样做是在隐藏内置类型。
回答by tdelaney
Martijn's answer is correct but also note that your original sample does more work than it needs to. dict.keys() and dict.values() both return lists and there is no reason to recreate them. The code could be:
Martijn 的回答是正确的,但也请注意,您的原始样本所做的工作比它需要的要多。dict.keys() 和 dict.values() 都返回列表,没有理由重新创建它们。代码可能是:
def run(my_dict):
    return my_dict.keys(), my_dict.values()
print run({"a": 1, "b": 2, "c": 3, "d": 4})
回答by John La Rooy
Another simpler (less chance for bugs) way to write your function
编写函数的另一种更简单(出现错误的机会更少)的方法
def run(my_dict):
    return zip(*my_dict.items())

