[英]python counter function unhashable type: 'list'
I want to use Counter function in my code, but im getting unhashable type error.我想在我的代码中使用计数器 function,但我得到了不可散列的类型错误。
lexicalClass = file.readlines()
for lex in lexicalClass:
newList = re.findall('\S+', lex)
for element in newList:
if len(re.findall('[a-z]+|[0-9]+', element)):
identifiers.append(re.findall('[a-z]+|[0-9]+', element))
I put in my txt file some strings, and im putting strings into "identifiers" list.我在我的 txt 文件中放入了一些字符串,并将字符串放入“标识符”列表中。 And now, when i try to use print(Counter(identifiers)) im getting this error:
现在,当我尝试使用 print(Counter(identifiers)) 时,我得到了这个错误:
Traceback (most recent call last):
File "C:\Users\jule\.spyder-py3\temp.py", line 93, in <module>
print(Counter(identifiers))
File "C:\Users\jule\anaconda3\lib\collections\__init__.py", line 552, in __init__
self.update(iterable, **kwds)
File "C:\Users\jule\anaconda3\lib\collections\__init__.py", line 637, in update
_count_elements(self, iterable)
TypeError: unhashable type: 'list'
All the objects in a Counter need to be hashable: Counter 中的所有对象都需要是可散列的:
A Counter is a dict subclass for counting hashable objects
Counter 是一个 dict 子类,用于计算可散列对象
The function re.findall()
gives you a list of strings. function
re.findall()
为您提供字符串列表。 You can update your code like that:您可以像这样更新您的代码:
identifiers.extend(re.findall('[a-z]+|[0-9]+', element))
or或者
identifiers += re.findall('[a-z]+|[0-9]+', element)
You can use a Counter on list of hashable items:您可以在可哈希项目列表上使用计数器:
>>> Counter(['a','b','c','a'])
Counter({'a': 2, 'b': 1, 'c': 1})
You cannot use a Counter on a list of unhashable items (and lists, being mutable, are not hashable):您不能在不可散列的项目列表上使用计数器(并且列表是可变的,不可散列):
>>> Counter([['a','b'],['c','a']])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python@3.9/3.9.1_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py", line 593, in __init__
self.update(iterable, **kwds)
File "/usr/local/Cellar/python@3.9/3.9.1_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py", line 679, in update
_count_elements(self, iterable)
TypeError: unhashable type: 'list'
Since re.findall
produces a list of tuples or a list of strings, you need to use the Counter directly on that list.由于
re.findall
生成元组列表或字符串列表,因此您需要直接在该列表上使用 Counter 。 If you use .append
with the list result from re.findall
you end up with a list of lists and you will see the error you are seeing.如果您将
.append
与re.findall
的列表结果一起使用,您最终会得到一个列表列表,您将看到您看到的错误。
Luckily, you can update a Counter directly.幸运的是,您可以直接更新计数器。 You can do something like:
您可以执行以下操作:
>>> txt='''\
... line 1
... Line 2
... LINE 3'''
>>> lines=Counter()
>>> lines.update(re.findall(r'^l', txt, flags=re.M))
>>> lines.update(re.findall(r'^L', txt, flags=re.M))
>>> lines.update(re.findall(r'^LINE', txt, flags=re.M))
>>> lines
Counter({'L': 2, 'l': 1, 'LINE': 1})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.