аватар question@mail.ru · 01.01.1970 03:00

The most common symbol

I want to make me introducing my word and I have written a symbol that appears in the word most often. It does not climb into my head how to do it. Please help. For example, we have a word: priority. We should deduce that the most common symbol is and, t. It is not necessary that it write the quantity, but only derived the symbol.

аватар answer@mail.ru · 01.01.1970 03:00
   from  collection  Import  COUNTERWORD =  c = counter (word)   print  (c.most_common ( 1 ) [ 0 ] [ 0 ])     

because. The most_common method retus a list of the most common values ​​(even if we requested one frequent value), you need to take the first element (we need the first [0] ). Each element in this list is a pair (an element, quantity) , so you need to take the first element again.

in general, in the word "priority" "there are 3 letters that are found twice (p, t), it will withdraw only one of them (" "p" "). Counter :

  word =  '"Priority'   # calculate the quantity the entries of each letter in the word  c =  dict  ()   for  letter  in  word:  c [letter] = c.get,  0 ) +  1   # .Get (letter, 0) will retu the value of the key or 0 if there is no such key    PRINT  (C) # {'p': 1, 'p': 2, 'and': 2, 'o': 1, 't': 2, 'e': 1}   # display the greatest value   # (more precise keys)    print  ( max  (c.items (), key =  lambda  item: item [ 1 ]) [ 0 ]) # p                                        

Latest

Similar