我正在尝试从文件中收集错误,并且需要对它们进行计数 这是我需要的输出:[ error1 : 6 , error2 : 3 , error3 : 2]
import os,sys
import collections
Data = []
errors = []
with open('out.txt') as f:
for line in f:
Data.append([word for word in line.strip().split("\t")])
for item in Data:
try:
date,error = item[0],item[5]
errors.append(error.split('\n'))
except IndexError:
print "A line in the file doesn't have enough entries."
counter = collections.Counter(errors)
print counter
最佳答案
你写:
errors.append(error.split('\n'))
现在 error.split('\n') 将**返回一个列表**。因此,
error` 不是一个字符串列表,而是一个字符串列表的列表。您可能想将其替换为:
errors <b>+=</b> error.split('\n')
现在 errors
将是一个字符串列表,而字符串是可哈希项。
关于python - 错误: unhashable type = 'list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45241260/