Python takes a word with a maximum number of occurrences in a string

Source: Internet
Author: User

#-*-coding:utf-8-*-#取一个字符串中最多出现次数的词ImportRe fromCollectionsImportCountermy_str="""Simple are better than Complex.complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren ' t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should is one--and preferably only one--obvious the-do it. Although that's obvious at first unless you ' re dutch.now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it could be a good idea. Namespaces is one honking great idea – let's do more than those!"""Word_count= Counter (Re.split ('[.,; ~!-<>/|@#$%^&*?\n\t]', my_str))#take non-empty top eight most wordsMost_11 = [x forXinchWord_count.most_common (8)ifX[0]! =""]#take the top eight most words directlyPrintWord_count.most_common (8)Print(Most_11) forIinchMost_11:PrintI[0]

Python takes a word with a maximum number of occurrences in a string

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.