项目频率计数在python [英] item frequency count in python
问题描述
我是一个python新手,所以也许我的问题是非常noob。
假设我有一个单词列表,我想找到每个单词出现在该列表中的次数。
显而易见的方式是:
I'm a python newbie, so maybe my question is very noob. Assume I have a list of words, and I want to find the number of times each word appears in that list. Obvious way to do this is:
words = "apple banana apple strawberry banana lemon"
uniques = set(words.split())
freqs = [(item, words.split.count(item)) for item in uniques]
print(freqs)
但我发现这个代码不是很好,因为这种方式通过单词列表运行两次,一次建立集合,出现次数。
当然,我可以写一个函数来运行列表和做计数,但那不会是那么pythonic。那么,是否有更高效的和蟒蛇的方式?
But I find this code not very good, because this way program runs through words list twice, once to build the set, and second time counting the number of appearances. Of course, I could write a function to run through list and do the counting, but that wouldn't be so pythonic. So, is there a more efficient and pythonic way?
推荐答案
defaultdict 来救援!
from collections import defaultdict
words = "apple banana apple strawberry banana lemon"
d = defaultdict(int)
for word in words.split():
d[word] += 1
这在O(n)中运行。
这篇关于项目频率计数在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!