This one was written in private plots.
>>> fromCollectionsImportCounter>>> C =Counter ()>>> forChinch 'Programming': .... c[ch]= C[ch] + 1...>>>Ccounter ({'g': 2,'m': 2,'R': 2,'a': 1,'I': 1,'o': 1,'N': 1,'P': 1})
Write an implementation, inherit dict, modify __getitem__
classCounter (dict):def __getitem__(Self,key):ifKey not inchSelf :return0Else: returnSuper (Counter,self).__getitem__(key) C=Counter () forChinch 'Programminga': C[ch]=c[ch]+1 forKeyinchSorted (C.keys ()):Print '{} : {}'. Format (Key,c[key])
Think of another thing, a bunch of data, sorted by the first letter, no case-sensitive
data=['a','Box','Tea','Apple','Banana','Banner','a test!','Zoo','Z']t={} forEinchData:t.setdefault (E[0].upper (), []). Append (e)Else: PrintT-------------------------------------get {'A': ['a','Apple','a test!'],'Z': ['Zoo','Z'],'B': ['Box','Banana','Banner'],'T': ['Tea']}
So
Data='programminga't={} for in data: T.setdefault (e,[]). Append (e)else: print for Key,value inch
-------------------------------------------------------
{' A ': 1, ' A ': 1, ' G ': 2, ' I ': 1, ' m ': 2, ' O ': 1, ' n ': 1, ' P ': 1, ' R ': 2}
Calculate word Frequency