Let’s say I have a list of unknown words:
l = [‘bike’, ‘blue car’, ‘wheel’, ‘car’]
I would like to write a script to determine which word appears twice in the list.
What is the best way to do that ?
>Solution :
sum([1 if 'car' in word else 0 for word in l])
As noted, this overcounts for words that have the letters ‘car’ in them like "care", "scar", etc…
In which case, it’s better to just split along spaces.
sum([1 if 'car' in word.split() else 0 for word in l])
More generic word count dictionary:
from collections import defaultdict
count = defaultdict(int)
for words in l:
for word in words.split():
count[word] += 1
print(count)