How do I count unique words using counter library in python?

2024/10/5 21:00:20

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?

Answer

The python Counter class takes an Iterable object as parameter. As you are giving it a String object:

Counter('like baby baby baby ohhh baby baby like nooo')

it will iterate over each character of the string and generate a count for each of the different letters. Thats why you are receiving

Counter({'b': 10, ' ': 8, 'a': 5, 'y': 5, 'o': 4, 'h': 3, 'l': 2, 'i': 2, 'k': 2, 'e': 2, 'n': 1})

back from the class. One alternative would be to pass a list to Counter. This way the Counter class will iterate each of the list elements and create the count you expect.

Counter(['like', 'baby', 'baby', 'baby', 'ohhh', 'baby', 'baby', 'like', 'nooo'])

That could also be simply achived by splitting the string into words using the split method:

Counter('like baby baby baby ohhh baby baby like nooo'.split())

Output

Counter({'baby': 5, 'like': 2, 'ohhh': 1, 'nooo': 1})
https://en.xdnf.cn/q/119738.html

Related Q&A

Need some debugging in my program: filling up SQL tables with data retrieved from a Python program

I am filling up SQL tables with data that I have retrieved from a Python program. I am using Visual Studio Code for the Python program and MySQL Workbench 8.0 for SQL. There are some errors in it that …

How do I create a magic square matrix using python

A basket is given to you in the shape of a matrix. If the size of the matrix is N x N then the range of number of eggs you can put in each slot of the basket is 1 to N2 . You task is to arrange the egg…

Ensuring same dimensions in Python

The dimensions of P is (2,3,3). But the dimensions of M is (3,3). How can I ensure that both P and M have the same dimensions i.e. (2,3,3). import numpy as np P=np.array([[[128.22918457, 168.52413295,…

how to stop tkinter timer function when i press button one more times?

id tried to use root.after_cancel(AFTER), but i dont know how.root.after_cancel(AFTER) AFTER = None def countdown(count,time,name):global AFTERtime[text] =name,":",datetime.fromtimestamp(cou…

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…

I dont understand why my script is not iterating through all string.split elements?

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending …

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes import numpy a…

Django Queryset foreign keys

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in use…