The Counter
class in the collections
module is purpose built to solve this type of problem:
from collections import Counter
words = "apple banana apple strawberry banana lemon"
Counter(words.split())
# Counter({'apple': 2, 'banana': 2, 'strawberry': 1, 'lemon': 1})
More Related Contents:
- How to count the frequency of the elements in an unordered list?
- How to create a counter for each new instance of string in python?
- I have a list of file names and I need to be able to count how many of the same file gets repeated for each file name [closed]
- How do I count the occurrences of a list item?
- How to sort a list of objects based on an attribute of the objects?
- Count the number of occurrences of a character in a string
- Count the frequency that a value occurs in a dataframe column
- How to find length of digits in an integer?
- Pandas ‘count(distinct)’ equivalent
- Fast way of counting non-zero bits in positive integer
- How to count the occurrence of certain item in an ndarray?
- How to find most common elements of a list? [duplicate]
- Why are str.count(”) and len(str) giving different output?
- How do I count the occurrence of a certain item in an ndarray?
- Group by & count function in sqlalchemy
- Count consecutive characters
- Get number of items from list (or other iterable) with certain condition
- Finding occurrences of a word in a string in python 3
- Sort list by frequency
- Python frequency detection
- How to count the number of files in a directory using Python
- How to obtain the total numbers of rows from a CSV file in Python?
- Row count in a csv file
- Pivot Tables or Group By for Pandas?
- Count() vs len() on a Django QuerySet
- Pivot Tables of Counts in Pandas DataFrame
- How to count digits, letters, spaces for a string in Python?
- Counting the number of True Booleans in a Python List
- Python – Finding word frequencies of list of words in text file
- What values are valid in Pandas ‘Freq’ tags?