python - 带有值和键的字典的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 .. .. ..
解决方案
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
推荐阅读
- json - 将 Dataframe 转换为具有指定架构的 JSON 数据
- javascript - 未捕获的 TypeError:reportValidity[] 不是函数
- angular - Observable 不刷新视图 - Angular 2+
- python - 将可变长度元组列表转换为字典
- machine-learning - keras 层中的尺寸不匹配
- python - Sklearn逻辑回归预测模型问题
- python - Python - MS SQL 运行多个查询而不会丢失以前的查询
- javascript - 如何在 3 个动态字段和另一个动态字段之间求和
- c++ - 我们可以在 C++ 中同一类的函数中声明一个类的对象吗?
- rust - 为什么我在返回值中得到“寿命不够长”?