Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python count the number of words used in Journey to the West?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to count the number of words used in Journey to the West by Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to count the number of words used in Journey to the West by Python"!

I. data

Xyj.txt, the text of Journey to the West, 2.2MB

Salute Master Wu Chengen, 4020 lines (paragraph)

II. Objectives

Statistics in Journey to the West:

1. How many different Chinese characters have appeared?

two。 How many times does each Chinese character appear?

3. Which Chinese characters appear most frequently?

Third, the content involved:

1. Read the file

two。 Use of dictionaries

3. The ordering of dictionaries

4. Write a file

IV. Effect

5. Source code

# coding:utf8import sysreload (sys) sys.setdefaultencoding ("utf8") fr = open ('xyj.txt', 'r') characters = [] stat = {} for line in fr: # remove the white space on both sides of each line line = line.strip () # skip the cycle if len (line) = = 0: continue # convert the text to unicode if it is blank It is convenient to process the Chinese character line = unicode (line) # iterate through each word of the line for x in xrange (0, len (line)): # remove punctuation marks and blank marks if line [x] in [',','\ tache,'\ nmarker,'. ,'(',)','(',')',':', '□','?' ,'!' ,.'] : continue # has not been recorded in characters if not line [x] in characters: characters.append (line [x]) # has not been recorded in stat if not stat.has_key (line [x]): stat [line [x]] = 0 # number of occurrences of Chinese characters plus 1 stat [line [x]] + = 1print len (characters) print len (stat) # lambda generates a temporary function # D represents each key-value pair of the dictionary D [0] sort in descending order for key,d [1], for value# reverse, for True: stat = sorted (stat.items (), key=lambda dvisd [1], reverse=True) fw = open ('result.csv',') for item in stat: # before performing string concatenation, you need to convert int to str fw.write (item [0] +','+ str (item [1]) +'\ n') fr.close () fw.close () here I believe you have a deeper understanding of "how Python counts the number of words used in Journey to the West". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report