Python Hamlet word Count Cloud

Source: Internet
Author: User
Tags gettext
Def getText ():  #赋予txt哈姆雷特小说-All English lowercase-full symbol variable space-Returns TXT        txt = open (' Hamlet.txt ', ' R '). Read () #注意文本的路径        txt = Txt.lower () #文本字符全部转变为小写字母 for        ch in '! ' #$%& () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ ':            txt = txt.replace (ch, "") #使用空格字符替换掉文本中所有特殊字符        return txthamlettxt = GetText () #调用函数words = Hamlettxt.split () #以空字符做列表返回counts = {} #创建一个空字典for word in words:    Counts[word] = Counts.get (word,0) + 1#get do not create word as the underlying value 0 + in Word 1, if get is on the value of Word add 1, and then update the dictionary items = list (Counts.items ()) #字典的键值对变成列表items. Sort (Key=lambda x:x[1],reverse=true) # Arranged in the number of occurrences, from large to small for I in range:    word,count = items[i]    print (' {0:<10},{1:>5} '. Format (Word,count))

  

Python Hamlet word Count Cloud

Related Article

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.