Suppose I have a list of words, and I want to know how many times each word appears in this list.
The obvious way to do this is:
words = "apple banana apple strawberry banana lemon" uniques = set(words.split()) freqs = [(item, words.split().count(item)) for item in uniques] print(freqs)
But I find this code not very good, because the program runs a list of words twice, once to build a set, and a second to count the number of occurrences.
Of course, I could write a function to start the list and count, but that would not be so Pythonic. So, is there a more efficient and Pythonic way?
python count frequency counting
Daniyar May 21 '09 at 15:04 2009-05-21 15:04
source share