Python Show-me-the-code The No. 0004 question statistical word

Source: Internet
Author: User

question No. 0004: any plain text file in English, counting the number of occurrences of the word.

Idea: match the words and numbers of the response with regular expressions, then let counter calculate the word frequency of the words, and then use the Most_common method to return a list of tuples that contain the word and the number of occurrences of that word in chronological order.

0004. Statistical words. PY

#!/usr/bin/env python#coding: Utf-8ImportRe fromCollectionsImportCounterfilesource ='/home/bill/desktop/testarticle ' def getmostcommonword(articlefilesource):    "Enter an English plain text file to count the number of occurrences of the word "Pattern =R ' [a-za-z]+|\$?\d+%?$ ']     withOpen (Articlefilesource) asF:r = Re.findall (Pattern,f.read ())returnCounter (R). Most_common ()if__name__ = =' __main__ ':PrintGetmostcommonword (Filesource)

One of the articles is a piece of news that you pick from the BBC.

Python Show-me-the-code The No. 0004 question statistical word

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.