假设我有一个名为 words 的单词列表,即 words = ["hello", "test", "string", "people", "hello", "hello"] 我想创建一个字典以获得词频.
假设字典被称为“计数”
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) 符号是什么意思?