python:不可散列的类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3410206/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:54:52  来源:igfitidea点击:

python: unhashable type error

python

提问by l--''''''---------''''''''''''

Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    do_work()
  File "C:\pythonwork\readthefile080410.py", line 14, in do_work
    populate_frequency5(e,data)
  File "C:\pythonwork\readthefile080410.py", line 157, in populate_frequency5
    data=medications_minimum3(data,[drug.upper()],1)
  File "C:\pythonwork\readthefile080410.py", line 120, in medications_minimum3
    counter[row[11]]+=1
TypeError: unhashable type: 'list'

I am getting the above error on this line:

我在这一行收到上述错误:

data=medications_minimum3(data,[drug.upper()],1)

(I have also tried drug.upper() without brackets)

(我也试过没有括号的 drug.upper())

Here is a preview of this function:

下面是这个函数的预览:

def medications_minimum3(c,drug_input,sample_cutoff): #return sample cut off for # medications/physician
  d=[]
  counter=collections.defaultdict(int)
  for row in c:
    counter[row[11]]+=1
  for row in c:
    if counter[row[11]]>=sample_cutoff:
      d.append(row) 
  write_file(d,'/pythonwork/medications_minimum3.csv')
  return d

Does anyone know what I am doing wrong here?

有谁知道我在这里做错了什么?

I know that what must be wrong is the way I am calling this function, because I call this function from a different location and it works fine:

我知道一定是我调用这个函数的方式有问题,因为我从不同的位置调用这个函数并且它工作正常:

d=medications_minimum3(c,drug_input,50)

Thank you very much for your help!

非常感谢您的帮助!

采纳答案by John La Rooy

I don't think converting to a tuple is the right answer. You need go and look at where you are calling the function and make sure that cis a list of list of strings, or whatever you designed this function to work with

我不认为转换为元组是正确的答案。您需要查看调用函数的位置,并确保这c是一个字符串列表,或者您设计此函数使用的任何内容

For example you might get this error if you passed [c]to the function instead of c

例如,如果您传递[c]给函数而不是c

回答by John Kugelman

counter[row[11]]+=1

You don't show what datais, but apparently when you loop through its rows, row[11]is turning out to be a list. Lists are mutable objects which means they cannot be used as dictionary keys. Trying to use row[11]as a key causes the defaultdictto complain that it is a mutable, i.e. unhashable, object.

你没有显示是什么data,但显然当你遍历它的行时,结果row[11]是一个list. 列表是可变对象,这意味着它们不能用作字典键。尝试row[11]用作键会导致defaultdict抱怨它是可变的,即不可散列的对象。

The easiest fix is to change row[11]from a listto a tuple. Either by doing

最简单的解决方法是row[11]从 a更改list为 a tuple。要么通过做

counter[tuple(row[11])] += 1

or by fixing it in the caller before datais passed to medications_minimum3. A tuple simply an immutable list, so it behaves exactly like a list does except you cannot change it once it is created.

或者在data传递给medications_minimum3. 元组只是一个不可变的列表,因此它的行为与列表完全一样,只是一旦创建就无法更改。

回答by S.Lott

  File "C:\pythonwork\readthefile080410.py", line 120, in medications_minimum3
    counter[row[11]]+=1
TypeError: unhashable type: 'list'

row[11]is unhashable. It's a list. That is precisely (and only) what the error message means. You might not like it, but that is the error message.

row[11]是不可哈希的。这是一个清单。这正是(并且仅)错误消息的含义。您可能不喜欢它,但这是错误消息。

Do this

做这个

counter[tuple(row[11])]+=1

Also, simplify.

另外,简化。

d= [ row for row in c if counter[tuple(row[11])]>=sample_cutoff ]

回答by chryss

As Jim Garrison said in the comment, no obvious reason why you'd make a one-element list out of drug.upper()(which implies drug is a string).

正如吉姆加里森在评论中所说的那样,没有明显的理由让你制作一个单元素列表drug.upper()(这意味着药物是一个字符串)。

But that's not your error, as your function medications_minimum3()doesn't even use the second argument (something you should fix).

但这不是您的错误,因为您的函数medications_minimum3()甚至不使用第二个参数(您应该解决的问题)。

TypeError: unhashable type: 'list'usually means that you are trying to use a list as a hash argument (like for accessing a dictionary). I'd look for the error in counter[row[11]]+=1-- are you sure that row[11]is of the right type? Sounds to me it might be a list.

TypeError: unhashable type: 'list'通常意味着您正在尝试使用列表作为哈希参数(例如访问字典)。我会寻找错误counter[row[11]]+=1- 您确定这row[11]是正确的类型吗?在我看来,这可能是一个列表。