Home > other >  For help, every items. Sort (key=lambda x: x [1])
For help, every items. Sort (key=lambda x: x [1])

Time:10-26

Def getText () :
TXT=open (" Hamlet. TXT ", "r"), read ()
TXT=TXT. The lower ()
For ch in '!" # $% & amp; () *, +, -,/:; <=& gt; ? @ [\ \] ^ _ '{|} ~' :
TXT=TXT. Replace (ch, "") # special characters in the text as Spaces
Return TXT
HamletTxt=getText ()
Words=hamletTxt. The split ()
Counts={}
For the word in words:
Counts [word]=counts. Get (word, 0) + 1
The items=list (counts. The items ())
items. Sort (key=lambda x: x [1], reverse=True)
# this step to see don't know much about
# lambda x: x [1] I understand input is a sequence of x, the output of x x [1] element
# but why items. Sort (key=lambda x: x [1], reverse=True) is the sorting sequence?
# items. Sort (key=lambda x: x [1]) what meaning be?
For I in range (10) :
Word, count=items [I]
Print (" {0: & lt; 10} {1: & gt; 5} ". The format (word count))
  • Related