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)符号是什么?
使用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)