【发布时间】:2011-03-08 17:18:10
【问题描述】:
假设我有一个名为 words 的单词列表,即 words = ["hello", "test", "string", "people", "hello", "hello"] 我想创建一个字典以便获取词频。
假设字典名为“counts”
counts = {}
for w in words:
counts[w] = counts.get(w,0) + 1
我不明白的唯一部分是counts.get(w.0)。书上说,通常你会使用 counts[w] = counts[w] + 1 但是当你第一次遇到一个新单词时,它不会在 counts 中,所以它会返回一个运行时错误。这一切都很好,但 counts.get(w,0) 到底是做什么的?具体来说,(w,0) 符号是什么意思?
【问题讨论】:
标签: python dictionary word-frequency