【问题标题】:Unhashable type: list不可散列类型:列表
【发布时间】:2017-07-11 10:02:46
【问题描述】:

我正在开发一个程序,该程序可以解析日志文件并返回 IP 地址和其他一些内容的热门点击。目前我遇到了麻烦,我无法将这个问题的任何答案解释为我现在正在做的事情。这是我所有的代码:

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'

有人可以帮助我吗?这令人沮丧。

【问题讨论】:

  • 列表是可变的,因此不可散列。因此它们不能用作字典中的键
  • 你能告诉我们ALL_ipAdd里面有什么吗?
  • @Ev.Kounis 有没有办法可以获取一个列表,将其转换为字典,然后对字典进行哈希处理?
  • @mattp341 您可以在将list 用作字典键之前将其转换为tuple
  • @StevenRumbalski 鉴于All_ipAdd 中只有一个列表,该解决方案仅适用于第一个切片{key:val for key, val in Counter(ALL_ipAdd[0]).most_common()}

标签: python list dictionary hashable


【解决方案1】:

从您的代码ALL_ipAdd = []ipAdd = []ALL_ipAdd.append(ipAdd) 我们可以得出结论ALL_ipAdd 是一个列表列表。 Counterdict 的子类型,它在计算项目之前对其进行哈希处理。列表不能被散列,因为它们是可变的(如果列表改变了,散列也会改变),因此列表不能被Counter 对象计数。

要解决这个问题,您可以在计算内部列表之前将其转换为元组:

ALL_ipAddDict = dict(Counter(map(tuple, ALL_ipAdd)).most_common())

【讨论】:

    【解决方案2】:

    这很正常。 ALL_ipAdd 是一个列表列表。 Counter 需要一个列表、一个字符串或任何其他可散列类型:)

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2014-02-16
      • 2018-12-26
      • 2019-01-07
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多