繁体   English   中英

字典中的Python增量值

[英]Python increment values in a dictionary

我正在尝试计算文本文件中的每个单词,并将单词附加到字典中作为键值对。 它引发了我这个错误:如果key不在wordDict中:TypeError:unhashable type:'list'另外,我想知道.split()是否很好,因为我的文本文件包含不同的标点符号。

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
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})

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

key是一个列表,您正在尝试查看列表是否在字典中,这等同于查看它是否为键之一。 字典键无法列出,因此出现“无法散列的类型”错误。

str.split返回单词列表

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

和列表或任何其他可变对象不能用作字典的键,这就是为什么会出现错误TypeError: unhashable type: 'list'

您需要对其进行迭代以包括其中的每一个,并且推荐的使用文件的方法with语句

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

可以通过使用Counter和对它的适当调用来缩短上述内容

from collections import Counter

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM