2017-12-21 1 views
0

메신저로 재미 있지는 않지만 재미있게하고 코드 샘플을 온라인으로 가져 오는 것이 좋습니다. , 내가 계속 '된다'그러나 총 단어 수 python은 계속 typeerror를 반환합니다. unhashable type : list

의지 '된다'단어

  • 분할

    1. 수를 총 단어 수
    2. 수 : 그래서 내가 다음을 수행하기로 결심 '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) 
      
  • +0

    오류 추적을 볼 수 있습니까? –

    +0

    트레이스 백 (최근 호출 마지막) : 파일 "C : \ Users \ user \ AppData \ Local \ Programs \ Python \ Python36-32 \ moves.py", 31 행, print (int (frequency [words])/wordCount) TypeError : unhashable type : 'list' –

    +0

    '단어'는 목록이 아닌 키워드라고 생각합니다. 목록을 dict 키워드로 사용할 수 없습니다. 디버거에서 '단어'를 확인하고 필요한 단어인지 확인하십시오. – Yohst

    답변

    1

    .

    list을 해싱 할 수 없기 때문에 words의 색인에서 마지막 줄의 frequency에있는 항목을 가져올 수 없다는 오류가 발생했습니다. 파이썬 사전이 어떻게 작동하는지 그리고 해시가 here을 사용하는 이유에 대해 자세히 읽을 수 있습니다.

    마지막 줄에 words 대신 다른 것을 넣으려는 것 같습니다. 'whereby' 일 수 있습니다.

    +1

    감사합니다. 마침내! :) –

    관련 문제