如何使用python中的计数器库计算唯一单词? [英] How do I count unique words using counter library in python?
问题描述
是python的新手,并尝试了各种库
im new to python and trying various libraries
from collections import Counter
print(Counter('like baby baby baby ohhh baby baby like nooo'))
当我打印此内容时,我收到的输出是:
When i print this the output I receive is:
Counter({'b': 10, ' ': 8, 'a': 5, 'y': 5, 'o': 4, 'h': 3, 'l': 2, 'i': 2, 'k': 2, 'e': 2, 'n': 1})
但是我想找到唯一单词的数量:
But I want to find the count of unique words:
#output example
({'like': 2, 'baby': 5, 'ohhh': 1, 'nooo': 1}, ('baby', 5))
如何做到这一点,另外,如果没有使用循环的计数器库,我还能做到这一点吗?
How can I do this, additionally can I do this without the counter library using loops?
推荐答案
使用collections.counter,首先应将字符串拆分成单词,例如 words ='like baby baby ohhh soou'.split()
然后将 words
变量输入计数器.
Using the collections.counter you should first split the string into words like so words = 'like baby baby ohhh so forth'.split()
Then feed the words
variable into the counter.
是的,您可以在没有集合模块(计数器对象)的情况下进行操作.有几种方法可以做到这一点.其中之一,可能不是最有效的之一:
Yes you can do it without collections module (counter object). There are several ways to do it. One of them, probably not the most efficient one is this:
words = 'like baby baby ohhh so forth'.split()
unique_words = set(words) # converting to set gets rid of duplicates
wordcount ={} # an epmty dict
for word in unique_words:
wordcount[word]=0 # set zero counter for each of the words
for word in words:
wordcount[word]+= 1 # for each occurrence of a word in the list made fro original string, find that key in dict and increment by 1
print(wordcount)
这篇关于如何使用python中的计数器库计算唯一单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!