count the frequency of words in python code example

Example 1: python count the frequency of words in a list

from collections import Counter
list1=['apple','egg','apple','banana','egg','apple']
counts = Counter(list1)

print(counts)
# Counter({'apple': 3, 'egg': 2, 'banana': 1})

Example 2: calculate term frequency python

from collections import Counter

# Counter token frequency from a sentence
sentence = "Texas A&M University is located in Texas"

term_frequencies = Counter(sentence.split())

Tags:

Java Example