python中的单词频率程序

1 python dictionary word-frequency

假设我有一个名为单词的单词列表,即单词= ["hello","test","string","people","hello","hello"]我想创建一个字典以获得单词频率.

假设字典被称为'计数'

counts = {}
for w in words:
    counts[w] = counts.get(w,0) + 1
Run Code Online (Sandbox Code Playgroud)

我真正理解的唯一部分是counts.get(w.0).书中说,通常你会使用计数[w] =计数[w] + 1但是第一次遇到一个新单词时,它不会计数,因此会返回运行时错误.这一切都很好,花花公子但是究竟做什么count.get(w,0)呢?具体来说,(w,0)符号是什么?

sam*_*ias 6

如果你有一个字典,那么这get()是一个方法,其中w包含你正在查找的单词的变量,0它是默认值.如果w字典中没有,则get返回0.


eat*_*eat 6

使用Python 2.7及更高版本的FWIW,您可能更愿意使用collections.Counter,例如:

In []: from collections import Counter
In []: c= Counter(["hello", "test", "string", "people", "hello", "hello"])
In []: c
Out[]: Counter({'hello': 3, 'test': 1, 'people': 1, 'string': 1})
Run Code Online (Sandbox Code Playgroud)