Python finds the 10 most frequently occurring words in a story

Source: Internet
Author: User
#!/usr/bin/python#filename:readlinepy.pyimport sys,reurldir=r "C:\python27\a.txt" distone={}numten=[] #先文档变成一个字典f = Open (Urldir, ' R ') for line in F.readlines (): #去掉非字符的符号 line    = re.sub (' \w ', "", line)        Lineone=line.split ()    For Keyone in Lineone:        if not Distone.get (keyone):            distone[keyone]=1                    Else:            distone[keyone]+= 1f.close () #整理前10出现的单词的次数for v in Distone.values ():    if v not in Numten:        numten.append (v) numten.sort () numten= NUMTEN[-10:] #将字典转为列表, in fact this can not turn. Direct traversal is also OK, did not think of what happens at the same time. Distone = sorted (Distone.iteritems (), Key=lambda d:d[1],reverse = True) #最后遍历, which can occur the same number of times. For i in Distone:    if i[1] in Numten:        print I

Python finds the 10 most frequently occurring words in a story

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.