Most Frequent Word in Strings List
We are given a list of strings we need to find the most frequent words from that particular list. For example, w = ["apple", "banana", "apple", "orange", "banana", "apple"] we need to find most frequent words in list which is 'apple' in this case.
Using Counter from collections
Counter class from the collections module counts occurrences of words in list. Using Counter.most_common(1) we can retrieve most frequent word along with its count.
from collections import Counter
# Define a list of words
w = ["apple", "banana", "apple", "orange", "banana", "apple"]
# Use Counter to count occurrences of each word and get the most common word
f = Counter(w).most_common(1)[0][0]
print(f)
Output
apple
Explanation:
- Counter(w) creates a dictionary-like object that counts occurrences of each word, and most_common(1) returns a list with most frequent word and its count.
- [0][0] extracts the most frequent word from tuple.
Using a Dictionary
We can use a dictionary to manually count word occurrences by iterating through list and updating count for each word with highest count is then identified by checking maximum value in dictionary.
w = ["apple", "banana", "apple", "orange", "banana", "apple"]
f = {}
for word in w:
f[word] = f.get(word, 0) + 1 # Increment count for each word
# Find the word with the highest frequency
m = max(f, key=f.get)
print(m)
Output
apple
Explanation:
- Dictionary f stores word counts using f.get(word, 0) + 1 to update occurrences for each word in list.
- max(f, key=f.get) finds the word with the highest frequency by selecting key with maximum count.
Using collections.defaultdict
defaultdict from the collections module simplifies counting by initializing missing keys with a default value. We can use it to store word frequencies and find most frequent word using max().
from collections import defaultdict
w = ["apple", "banana", "apple", "orange", "banana", "apple"]
freq = defaultdict(int)
for word in w:
freq[word] += 1
f = max(freq, key=freq.get)
print(f)
Output
apple
Explanation:
- defaultdict(int) automatically initializes missing keys with a default value of 0 making it easier to count word occurrences without checking for key existence.
- max(freq, key=freq.get) finds word with highest count by selecting key with maximum value.