SoFunction
Updated on 2024-11-18

Python reads an English file and records the number of times each word occurs and outputs it in descending order.

This article example describes Python read English files and record the number of times each word appears after the descending output. Shared for your reference, as follows:

Periods, commas and exclamation points that appear in the text are treated accordingly

sortedSort Function Usage:

on the basis ofvalueThe values are listed in descending order:

sorted((),key=lambda k:k[1],reverse=True)

on the basis ofvalueThe values are sorted in ascending order:

sorted((),key=lambda k:k[1],reverse=False)

or

sorted((),key=lambda k:k[1])

on the basis ofkeyThe values are listed in descending order:

sorted((),key=lambda k:k[0],reverse=True)

on the basis ofkeyThe values are listed in ascending order:

sorted((),key=lambda k:k[0])

or

sorted((),key=lambda k:k[0],reverse=False)

Python Example:

# -*- coding:utf-8 -*-
#! python2
file_object=open("")
dict={}
for line in file_object:
  line=(","," ")
  line=("."," ")
  line=("!"," ")
  strs= ();
  for str in strs:
    if dict.has_key(str):
      dict[str]+=1
    else:
      dict[str]=1
result=sorted((),key=lambda k:k[1],reverse=True)
print result

Documentation:

We are busy all day, like swarms of flies without souls, noisy, restless, unable to hear the voices of the soul. As time goes by, childhood away, we grew up, years away a lot of memories, once have also eroded the bottom of the childish innocence, we regardless of the shackles of mind, indulge in the world buckish, focus on the beneficial principle, we have lost themselves.

Run results:

[('the', 7), ('of', 6), ('we', 3), ('have', 2), ('away', 2), ('flies', 1), ('regardless', 1), ('restless', 1), ('up', 1), ('indulge', 1), ('mind', 1), ('all', 1), ('voices', 1), ('are', 1), ('in', 1), ('We', 1), ('busy', 1), ('shackles', 1), ('also', 1), ('memories', 1), ('by', 1), ('to', 1), ('unable', 1), ('goes', 1), ('themselves', 1), ('lot', 1), ('on', 1), ('buckish', 1), ('focus', 1), ('souls', 1), ('hear', 1), ('innocence', 1), ('world', 1), ('years', 1), ('day', 1), ('noisy', 1), ('a', 1), ('eroded', 1), ('grew', 1), ('like', 1), ('lost', 1), ('swarms', 1), ('bottom', 1), ('soul', 1), ('As', 1), ('without', 1), ('principle', 1), ('beneficial', 1), ('time', 1), ('childish', 1), ('childhood', 1), ('once', 1)]

PS: Here we recommend 2 more related statistical tools for your reference:

Online word count tool:
http://tools./code/zishutongji

Online character counting and editing tool:
http://tools./code/char_tongji

Readers interested in more Python related content can check out this site's topic: theSummary of Python file and directory manipulation techniques》、《Summary of Python text file manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.