Python learning experience (1): word frequency statistics, top-down design

Today’s program is a Hamlet word frequency statistics, that is, statistics of the frequency of each word in Hamlet. For the first time, I tried to use the top-down design method and the top-down execution method. A lot of errors have been recorded during this period, so as to avoid future recidivism.

Before programming, intercept one part of Hamlet online.It should be noted that when the txt type is saved, the encoding mode selects’utf-8′. Figure:

Next, analyze the whole programming topic and list the steps:

The first step is to open the file and read it and separate every word.

The second step is to change all the letters into lowercase, taking into account the case.

The third step is to measure the frequency of each word and output it to a high level.

Therefore, the main function main () is:

split()The swapcase () function reads and splits the words in the file to get a list of splitwords, and the swapcase () function converts all the words into lowercase mode to get a new list. The countTimes () function statistics the frequency of occurrences of each word in the form of dictionaries.Outputs () function and sorts () function sort output.

The code is as follows:

Many errors were found when debugging the program.

1.Object usage: after s1.replace, no updated string is assigned to S1. Return ls.sort returns directly, resulting in a null value returned. Instead of returning a method procedure, LS should be returned.

2.When cycling, range is always added less.

3.The defined split () function cannot be segmented by’\n and needs to be separated by spaces.

The result of the program is as follows: there is no format output.

We need more practice in the future.

 

Leave a Reply

Your email address will not be published. Required fields are marked *