Calculate word Frequency

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.