2017-02-21 13 views
1

나는 로그 파일을 분석하고 IP 주소와 몇 가지 다른 것들에 대한 최고 조회수를 반환하는 프로그램을 만들고있다. 현재 문제가있어 현재이 문제에 대한 답변을 해석 할 수 없습니다. 이것은 내 모든 코드 : 이제Unhashable type : list

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) 

어디 문제가 있어요 것은 다음 줄에 있습니다

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_ipAddDict = dict(Counter(ALL_ipAdd).most_common()) 

내가 전체 프로그램을 실행 출력이 있습니다

누군가 나를 도울 수 있습니까? 이것은 실망 스럽다.

+2

리스트 해쉬되지 그러므로 가변하고 있습니다. 결과적으로 사전의 키로 사용할 수 없습니다. –

+0

'ALL_ipAdd' 안에 무엇이 있는지 보여줄 수 있습니까? – Bahrom

+0

@ Ev.Kounis 목록을 가져 와서 사전으로 변환 한 다음 사전을 해싱 할 수있는 방법이 있습니까? – mattp341

답변

-2

그건 정상입니다. ALL_ipAdd은 목록의 목록입니다. Counter는 목록, 문자열 또는 코드에서 다른 해쉬 유형 :

+0

나는리스트가 해시 가능하지 않다는 것을 분명히 알았다고 생각했다. –

3

ALL_ipAdd = []ipAdd = []ALL_ipAdd.append(ipAdd) 우리가 ALL_ipAdd 목록 목록이라고 결론을 내릴 수있다 필요합니다. Counterdict의 하위 유형으로, 항목을 계산하기 전에 해시합니다. 목록을 변경할 수 있기 때문에 해시 할 수 없습니다 (목록이 변경되면 해시가 변경됨) Counter 개체로 목록을 계산할 수 없습니다.

당신이 그들을 계산하기 전에 튜플에 내부 목록을 변환 할 수 있습니다이 문제를 해결하려면 다음

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