将列表元素追加到字典

时间:2019-09-28 16:25:21

标签: python list dictionary

我有一个随机的文本文件,想将所有单词都放入字典中,因为我必须对它们进行计数。

raw_data = open("ipsum.txt", "r").readlines()

data = []
word_dict = {

    'word' : 0

}

for lines in raw_data:

   lines = lines.strip('\n')
   data.append(lines.split(" "))


print(data)

for word in data:
    if word not in word_dict:
        word_dict[word] = 0

但是我总是收到以下错误消息:

  if word not in word_dict:
TypeError: unhashable type: 'list'

我不知道如何继续。

1 个答案:

答案 0 :(得分:2)

以下是更简单,更清洁的解决方案:

file = open("ipsum.txt", "r")
wordcount={}
for word in file.read().split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1
for k,v in wordcount.items():
    print k,v

更Python化的方式是:

from collections import Counter
file = open("ipsum.txt", "r")
wordcount = Counter(file.read().split())

您也可以使用

对它们进行排序
wordcount = sorted(wordcount.items(), key=lambda x:x[1], reverse=true)
相关问题