Here is a practical example of usage of Counter from the collection module.
The function count_letters counts the letters in a text and count_words counts the words. It’s quite lapalissian.
# how many words we use in a text from collections import Counter tx = """This library uses pygame and lets you use some widgets ready to be used in your games. Installation If you do not have pygame pip install pygame If you want to use pygame 2 with python 3.8 use this pip install pygame==2.0.0.dev10 Check for more recent version of this. Maybe there is an official version of pygame 2 for python 3.8 or more when you read this. To install pygame_gui: pip install pygame_gui Your first program Taken from the official documentation, here is how you make a button in a window. The repository og pygame_gui is here.""" def count_letters(text): return Counter(text) def count_words(text): return Counter(text.split()) def show_unsorted(dic_l): for x in dic_l: print(x, dic_l[x]) print(dic_l) def show_sorted(dic_l, reverse=True): for x in sorted(dic_l, key=dic_l.get, reverse=reverse): print(x, dic_l[x]) print(dic_l) dic_l = count_letters(tx) dic_w = count_words(tx) [show_sorted(x) for x in (dic_l, dic_w)]
The output of the script
87 e 44 o 37 i 33 a 32 t 27 s 26 r 23 n 23 y 21 p 19 u 17 m 17 h 16 g 16 l 14 f 12 . 10 10 d 9 w 7 c 5 T 4 b 4 v 4 I 3 2 3 0 3 k 3 _ 3 3 2 8 2 = 2 1 1 C 1 M 1 : 1 Y 1 , 1 Counter({' ': 87, 'e': 44, 'o': 37, 'i': 33, 'a': 32, 't': 27, 's': 26, 'r': 23, 'n': 23, 'y': 21, 'p': 19, 'u': 17, 'm': 17, 'h': 16, 'g': 16, 'l': 14, 'f': 12, '.': 10, '\n': 10, 'd': 9, 'w': 7, 'c': 5, 'T': 4, 'b': 4, 'v': 4, 'I': 3, '2': 3, '0': 3, 'k': 3, '_': 3, '3': 2, '8': 2, '=': 2, '1': 1, 'C': 1, 'M': 1, ':': 1, 'Y': 1, ',': 1}) pygame 5 you 5 install 4 use 3 pip 3 is 3 to 2 in 2 If 2 2 2 python 2 3.8 2 for 2 more 2 version 2 of 2 this. 2 official 2 pygame_gui 2 a 2 This 1 library 1 uses 1 and 1 lets 1 some 1 widgets 1 ready 1 be 1 used 1 your 1 games. 1 Installation 1 do 1 not 1 have 1 want 1 with 1 this 1 pygame==2.0.0.dev10 1 Check 1 recent 1 Maybe 1 there 1 an 1 or 1 when 1 read 1 To 1 pygame_gui: 1 Your 1 first 1 program 1 Taken 1 from 1 the 1 documentation, 1 here 1 how 1 make 1 button 1 window. 1 The 1 repository 1 og 1 here. 1 Counter({'pygame': 5, 'you': 5, 'install': 4, 'use': 3, 'pip': 3, 'is': 3, 'to': 2, 'in': 2, 'If': 2, '2': 2, 'python': 2, '3.8': 2, 'for': 2, 'more': 2, 'version': 2, 'of': 2, 'this.': 2, 'official': 2, 'pygame_gui': 2, 'a': 2, 'This': 1, 'library': 1, 'uses': 1, 'and': 1, 'lets': 1, 'some': 1, 'widgets': 1, 'ready': 1, 'be': 1, 'used': 1, 'your': 1, 'games.': 1, 'Installation': 1, 'do': 1, 'not': 1, 'have': 1, 'want': 1, 'with': 1, 'this': 1, 'pygame==2.0.0.dev10': 1, 'Check': 1, 'recent': 1, 'Maybe': 1, 'there': 1, 'an': 1, 'or': 1, 'when': 1, 'read': 1, 'To': 1, 'pygame_gui:': 1, 'Your': 1, 'first': 1, 'program': 1, 'Taken': 1, 'from': 1, 'the': 1, 'documentation,': 1, 'here': 1, 'how': 1, 'make': 1, 'button': 1, 'window.': 1, 'The': 1, 'repository': 1, 'og': 1, 'here.': 1})
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame