2016-12-15 5 views
-1

私は、テキストファイルからすべての単語を数え、単語とカウントをキーと値のペアとして辞書に追加しようとしています。これは私にこのエラーを投げます:wordでないなら、wordDict: TypeError:unhashable type: 'list' また、私のテキストファイルには異なる句読点が含まれているので、.split()が良いと思っています。辞書内のPythonのインクリメント値

fileref = open(mypath + '/' + i, 'r') 
wordDict = {} 
for line in fileref.readlines(): 
    key = line.split() 
    if key not in wordDict: 
     wordDict[key] = 1 
    else: 
     wordDict[key] += 1 
+0

_additional_セパレータを使いたい場合は、 'split'の前に' line.replace(other、 '') 'を使うことができます。 – MSeifert

答えて

2
from collections import Counter 
text = '''I am trying to count every word from text files and appending the word and count to a dictionary as the key-value pairs. It throws me this error: if key not in wordDict: TypeError: unhashable type: 'list' Also, I am wondering of .split() is good because my text files contain different punctuation marks. Thanks ahead for those who help!''' 

split_text = text.split() 
counter = Counter(split_text) 
print(counter) 

アウト:

Counter({'count': 2, 'and': 2, 'text': 2, 'to': 2, 'I': 2, 'files': 2, 'word': 2, 'am': 2, 'the': 2, 'dictionary': 1, 'a': 1, 'not': 1, 'in': 1, 'ahead': 1, 'me': 1, 'trying': 1, 'every': 1, '.split()': 1, 'type:': 1, 'my': 1, 'punctuation': 1, 'is': 1, 'key': 1, 'error:': 1, 'help!': 1, 'those': 1, 'different': 1, 'throws': 1, 'TypeError:': 1, 'contain': 1, 'wordDict:': 1, 'appending': 1, 'if': 1, 'It': 1, 'Also,': 1, 'unhashable': 1, 'from': 1, 'because': 1, 'marks.': 1, 'pairs.': 1, 'this': 1, 'key-value': 1, 'wondering': 1, 'Thanks': 1, 'of': 1, 'good': 1, "'list'": 1, 'for': 1, 'who': 1, 'as': 1}) 
+0

この例は、複数行の例を使用する方が良い場合がありますが、str.splitlines()を使用することができます。 :) – Copperfield

+0

@ Copperfieldはチップのおかげです。 –

0

keyリストであり、あなたは、リストはそれがキーのいずれかであれば見に相当し、辞書にあるかどうかを確認しようとしています。ディクショナリキーはリストになるので、 "unhashable type"というエラーになりません。

1

keyは、現在の行にある空白で区切られた単語のリストです。そのリストを繰り返し処理する必要があります。

for line in fileref: 
    keys = line.split() 
    for key in keys: 
     if key not in wordDict: 
      wordDict[key] = 1 
     else: 
      wordDict[key] += 1 

これはsetdefault方法又はcollectionsモジュールからdefaultdictのいずれかを使用することによって大幅にクリーンアップすることができます。両方とも、dictにまだ含まれていない場合は、初期値のキーを自動的に追加することによって、キーを明示的にチェックすることを避けることができます。

for key in keys: 
    wordDict.setdefault(key, 0) += 1 

または

from collections import defaultdict 
wordDict = defaultdict(int) # Default to 0, since int() == 0 

... 

    for key in keys: 
     wordDict[key] += 1 
0

str.splitリターン言葉

>>> "hello world".split() 
['hello', 'world'] 
>>> 

とリストやその他の変更可能なオブジェクトは、辞書のキーとして使用することはできない、とのリストが、それはなぜですエラーTypeError: unhashable type: 'list'を取得します。あなたはそれらの一つ一つを含めるためにそれを反復処理する必要が

は、またfileで動作するように推奨される方法はwith statement

wordDict = {} 
with open(mypath + '/' + i, 'r') as fileref: 
    for line in fileref: 
     for word in line.split(): 
      if word not in wordDict: 
       wordDict[word] = 1 
      else: 
       wordDict[word] += 1 

である上記の使用Counterappropriateコールを短縮することができます

from collections import Counter 

with open(mypath + '/' + i, 'r') as fileref:  
    wordDict = Counter(word for line in fileref for word in line.split()) 
+0

うまく働いて、ありがとう! – Yolanda