Count frequency of words in a list and sort by frequency Count frequency of words in a list and sort by frequency python python

Count frequency of words in a list and sort by frequency


use this

from collections import Counterlist1=['apple','egg','apple','banana','egg','apple']counts = Counter(list1)print(counts)# Counter({'apple': 3, 'egg': 2, 'banana': 1})


You can use

from collections import Counter

It supports Python 2.7,read more information here

1.

>>>c = Counter('abracadabra')>>>c.most_common(3)[('a', 5), ('r', 2), ('b', 2)]

use dict

>>>d={1:'one', 2:'one', 3:'two'}>>>c = Counter(d.values())[('one', 2), ('two', 1)]

But, You have to read the file first, and converted to dict.

2.it's the python docs example,use re and Counter

# Find the ten most common words in Hamlet>>> import re>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())>>> Counter(words).most_common(10)[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]


words = file("test.txt", "r").read().split() #read the words into a list.uniqWords = sorted(set(words)) #remove duplicate words and sortfor word in uniqWords:    print words.count(word), word