메신저로 재미 있지는 않지만 재미있게하고 코드 샘플을 온라인으로 가져 오는 것이 좋습니다. , 내가 계속 '된다'그러나 총 단어 수 python은 계속 typeerror를 반환합니다. unhashable type : list
의지 '된다'단어
- 수를 총 단어 수
- 수 : 그래서 내가 다음을 수행하기로 결심 'unhashable 형식 :'목록 '지고. frequency_list를 사용하기 때문에 이것이라고 가정합니다. 그러나 나는 그것에 대해 어떻게해야하는지 이해하지 못합니다.
오류 추적 : 역 추적 (마지막으로 가장 최근 통화) : 파일 "C : \ 사용자 \ 사용자 \의 AppData \ 로컬 \ 프로그램 \ 파이썬 \ Python36-32 \ whereby.py", 라인 (31), 인쇄에서 (INT (주파수 [단어])/단어 수) 형식 오류 : unhashable 유형 : '목록'당신이 분할에서
list
객체로words
을 설정하는 당신의words = p.split()
라인에서import re import string frequency = {} wherebyity = {} document_text = open('C:/Users/user/desktop/text.txt', 'r') text_string = document_text.read().lower() match_pattern = re.findall('whereby', text_string) for word in match_pattern: count = frequency.get(word, 0) frequency[word] = count + 1 frequency_list = frequency.keys() for words in frequency_list: print (words, frequency[words]) print (type(frequency)) print (type(frequency[words])) print (frequency) with open('C:/Users/user/desktop/text.txt', 'r') as f: p = f.read() # p contains contents of entire file # logic to compute word counts follows here... words = p.split() wordCount = len(words) print ("The total word count is:", wordCount) print (type(wordCount)) print(int(frequency[words])/wordCount)
오류 추적을 볼 수 있습니까? –
트레이스 백 (최근 호출 마지막) : 파일 "C : \ Users \ user \ AppData \ Local \ Programs \ Python \ Python36-32 \ moves.py", 31 행, print (int (frequency [words])/wordCount) TypeError : unhashable type : 'list' –
'단어'는 목록이 아닌 키워드라고 생각합니다. 목록을 dict 키워드로 사용할 수 없습니다. 디버거에서 '단어'를 확인하고 필요한 단어인지 확인하십시오. – Yohst