Python中的词频程序 [英] word frequency program in python

查看:66
本文介绍了Python中的词频程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个单词列表,称为单词,即单词= ["hello","test","string","people","hello","hello"],我想创建一个字典以获取单词频率.

Say I have a list of words called words i.e. words = ["hello", "test", "string", "people", "hello", "hello"] and I want to create a dictionary in order to get word frequency.

比方说字典叫做计数"

Let's say the dictionary is called '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)表示法是什么?

The only part of this I don't really understand is the counts.get(w.0). The book says, normally you would use counts[w] = counts[w] + 1 but the first time you encounter a new word, it won't be in counts and so it would return a runtime error. That all fine and dandy but what exactly does counts.get(w,0) do? Specifically, what's the (w,0) notation all about?

推荐答案

如果您有字典,则 get()是一种方法,其中 w 是持有变量的变量.您要查找的单词,默认值为 0 .如果字典中不存在 w ,则 get 返回 0 .

If you have a dictionary, get() is a method where w is a variable holding the word you're looking up and 0 is the default value. If w is not present in the dictionary, get returns 0.

这篇关于Python中的词频程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆