counting

Item frequency count in Python

强颜欢笑 提交于 2019-11-26 00:55:02
问题 Assume I have a list of words, and I want to find the number of times each word appears in that list. An 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 through the word list twice, once to build the set, and a second time to count the number of appearances. Of course, I could write a function

Item frequency count in Python

最后都变了- 提交于 2019-11-25 19:26:32
Assume I have a list of words, and I want to find the number of times each word appears in that list. An 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 through the word list twice, once to build the set, and a second time to count the number of appearances. Of course, I could write a function to run through the list and do the counting, but that wouldn't be so Pythonic. So, is there a more