首页 > 解决方案 > 带有值和键的字典的python词频

问题描述

我是python的第一次学习者,我了解如何使用词频来计算列表中每个唯一变量的数量,就像这样

sentence = ['hello', 'people', 'are', 'the', 'most', 'common', 'word', 'people', 'use', 'for', 'language ', 'learning']

words_freq ={} #dictionary for the counts
for word in sentence:
    if word not in words_freq:

        words_freq[word] =1
    else:

        words_freq[word] +=1

print (words_freq)

但是,我想知道如何通过使用双 for 循环在字典上执行单词频率?

例如,我有一本这样的字典

Food = {
2015: ["Apple", "Milk", "Cookie", "Banana", "Orange" ],
2016: ["Potato", "Orange", "Chocolate", "Milk", "Mango"],
2017: ["Fish", "Potato", "Orange", "Mango", "Banana"],
2018: ["Beef", "Pork", "Fish", "Apple", "Cookie"],
2019: ["Pork", "Orange", "Apple", "Mango", "Chocolate"]
}

如何做一个单词频率/计数并打印这样的东西?还是以列表形式存储最高值?苹果:3 牛奶:2 橙子:3 .. .. ..

标签: python

解决方案


defaultdict 对于您的用例会非常优雅,它为 int 的给定类型创建默认字典 - 对于 int 值,默认值为 0,它使您可以编写更少的代码

https://docs.python.org/3.3/library/collections.html#collections.defaultdict

from collections import defaultdict

def get_freq(food_dict: dict) -> dict:
    freq = defaultdict(int)
    for year, lst in food_dict.items():
        for elem in lst:
            freq[elem] += 1
    return freq

推荐阅读