相关疑难解决方法(0)

python中的项频率计数

我是一个蟒蛇新手,所以也许我的问题非常棒.假设我有一个单词列表,我想找到每个单词出现在该列表中的次数.明显的做法是:

words = "apple banana apple strawberry banana lemon"
uniques = set(words.split())
freqs = [(item, words.split().count(item)) for item in uniques]
print(freqs)
Run Code Online (Sandbox Code Playgroud)

但是我发现这个代码不是很好,因为这种方式程序运行两次单词列表,一次构建集合,第二次计算出现次数.当然,我可以编写一个函数来运行列表并进行计数,但这不会那么pythonic.那么,有更高效和pythonic的方式吗?

python frequency count counting

54
推荐指数
5
解决办法
11万
查看次数

标签 统计

count ×1

counting ×1

frequency ×1

python ×1