Python 不可损坏类型:列表
我正在开发一个程序,它可以解析日志文件,并返回IP地址的最热门点击数和其他一些东西。目前我遇到了麻烦,我无法将这个问题的任何答案解释为我现在正在做的事情。这是我的全部代码:Python 不可损坏类型:列表,python,list,dictionary,hashable,Python,List,Dictionary,Hashable,我正在开发一个程序,它可以解析日志文件,并返回IP地址的最热门点击数和其他一些东西。目前我遇到了麻烦,我无法将这个问题的任何答案解释为我现在正在做的事情。这是我的全部代码: import gzip from collections import Counter logFileName = open('C:\\Users\\Pawlaczykm\\Desktop\\fileNames.txt', 'r') ipAdd = [] landingPages = [] ALL_ipAdd = [] AL
import gzip
from collections import Counter
logFileName = open('C:\\Users\\Pawlaczykm\\Desktop\\fileNames.txt', 'r')
ipAdd = []
landingPages = []
ALL_ipAdd = []
ALL_landingPages = []
# everything after this line gets done to all files
for line in logFileName.readlines():
# rstrip removes a blank line from output
# print 'Summary of: ' + line.rstrip()
# use gzip to decompress the file
with gzip.open('C:\\Users\\Pawlaczykm\\Desktop\\logFiles\\' + line.rstrip() + '.gz', 'rb') as f:
# we extract the ip addresses in lines 15-18
for eachLine in f:
parts = eachLine.split('\t')
if len(parts) > 1:
ipAdd.append(parts[2])
ALL_ipAdd.append(ipAdd)
# use gzip to decompress the file
with gzip.open('C:\\Users\\Pawlaczykm\\Desktop\\logFiles\\' + line.rstrip() + '.gz', 'rb') as f:
# we extract the landing pages
for eachLine in f:
parts = eachLine.split('\t')
if len(parts) > 1:
variable = parts[8].split('?')[0]
landingPages.append(variable)
v): (-v, k))[:10]
ALL_landingPages.append(landingPages)
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
sortedALL_ipAdd = sorted(ALL_ipAddDict.iteritems(), key=lambda (k, v): (-v, k))[:10]
print 'Top IPs of all files'
print(sortedALL_ipAdd)
ALL_LandingPageDict = dict(Counter(ALL_landingPages).most_common())
sortedALL_LandingPage = sorted(ALL_LandingPageDict.iteritems(), key=lambda (k, v): (-v, k))[:10]
print 'Top landing pages of all files'
print (sortedALL_LandingPage)
现在我遇到的问题是:
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
当我运行整个程序时,输出如下:
Traceback (most recent call last):
File "C:/Users/Pawlaczykm/PycharmProjects/LogParse/parseText.py", line 35, in <module>
ALL_ipAddDict = dict(Counter(ALL_ipAdd).most_common())
File "C:\Python27\lib\collections.py", line 477, in __init__
self.update(*args, **kwds)
File "C:\Python27\lib\collections.py", line 567, in update
self[elem] = self_get(elem, 0) + 1
TypeError: unhashable type: 'list'
回溯(最近一次呼叫最后一次):
文件“C:/Users/Pawlaczykm/PycharmProjects/LogParse/parseText.py”,第35行,在
ALL_IPADDICT=dict(计数器(ALL_ipAdd).most_common())
文件“C:\Python27\lib\collections.py”,第477行,在\uuu init中__
自我更新(*args,**kwds)
更新中第567行的文件“C:\Python27\lib\collections.py”
self[elem]=self_get(elem,0)+1
TypeError:不可损坏的类型:“列表”
有人能帮我吗?这很令人沮丧。这很正常<代码>所有ipAdd是一个列表列表<代码>计数器需要一个列表、一个字符串或任何其他可哈希类型:)来自您的代码
ALL\u ipAdd=[]
和ipAdd=[]
和ALL\u ipAdd.append(ipAdd)
我们可以断定ALL\u ipAdd
是一个列表<代码>计数器是dict的子类型,它在对项目进行计数之前对其进行散列。不能对列表进行散列,因为它们是可变的(如果列表更改,散列将更改),因此列表不能由计数器
对象计数
要解决此问题,可以在计算内部列表之前将其转换为元组:
ALL_ipAddDict = dict(Counter(map(tuple, ALL_ipAdd)).most_common())
列表是可变的,因此不可散列。因此,它们不能用作字典中的键。你能告诉我们
ALL\u ipAdd
中的内容吗?@Ev.Kounis有什么方法可以将列表转换成字典,然后对字典进行散列?@mattp341您可以将列表
转换为元组
,然后再将其用作字典key@StevenRumbalski如果All_ipAdd
中只有一个列表,那么只要第一个片段{key:val代表key,val代表Counter(All_ipAdd[0]),该解决方案就可以工作。most_common()}