I'm a python newbie, so maybe my question is very noob. Assume I have a list of words, and I want to find the number of times each word appears in that list. 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 this way program runs through words list twice, once to build the set, and second time counting the number of appearances. Of course, I could write a function to run through list and do the counting, but that wouldn't be so pythonic. So, is there a more efficient and pythonic way?
I happened to work on some Spark exercise, here is my solution.
**#output of the above **
Use reduce() to convert the list to a single dict.
returns
Can't you just use count?
If you are using python 2.7+/3.1+, there is a Counter Class in the collections module which is purpose built to solve this type of problem:
Since both 2.7 and 3.1 are still in beta it's unlikely you're using it, so just keep in mind that a standard way of doing this kind of work will soon be readily available.
Hope this helps!
Standard approach:
Groupby oneliner: