Python basic exercise: calculate the characters that appear most times in the list

Give you a text containing different English letters and punctuation marks. If you want to find the most letters, the returned letters must be in lowercase. When checking the most desired letters, they are not case sensitive, so "a" = = "a" in your search. Make sure you don't count punctuation, numbers and spaces, just letters.

If you find two or more letters with the same frequency, return the first letter in the alphabet. For example – "one" contains "o", "n", "e" once per letter, so we choose "e".

  • Input: text for analysis (str, unicode)

  • Output: the lowercase form of the most common letters.

example:

get_max_value("Hello World!") == "l"
get_max_value("How do you do?") == "o"
get_max_value("One") == "e"
get_max_value("Oops!") == "o"
get_max_value("AAaooo!!!!") == "a"
get_max_value("abe") == "a"

How to use: for most decryption tasks, you need to know how often various letters appear in a text. For example, if we know the frequency of which letter appears, we can easily crack a simple addition password or replacement password. This is an interesting thing for language experts!

Premise: the password only contains ASCII symbols 0 < len (text) ≤ 105

General method:

Use the Counter in the collections tool to sort the frequency of elements in the list. The Counter return value is a Counter object arranged in descending order according to the occurrence frequency of elements. It is a subclass of the dictionary, so you can use the methods of the dictionary.

'''
No one answers any questions? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded partners to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
import re
from collections import Counter

def get_max_value(text):
    text = text.lower()
    result = re.findall('[a-zA-Z]', text)  # Remove symbols from the list
    count = Counter(result)  # Counter({'l': 3, 'o': 2, 'd': 1, 'h': 1, 'r': 1, 'e': 1, 'w': 1})
    count_list = list(count.values())
    max_value = max(count_list)
    max_list = []
    for k, v in count.items():
        if v == max_value:
            max_list.append(k)
    max_list = sorted(max_list)
    return max_list[0]

Streamlining method:

Counter is also used, but the function can be more refined through list derivation. This is the benefit of list derivation.

from collections import Counter
​
def get_max_value(text):
    count = Counter([x for x in text.lower() if x.isalpha()])
    m = max(count.values())
    return sorted([x for (x, y) in count.items() if y == m])[0]

Best method:

I have to praise this method. It's really crisp. It cleverly uses the max() function.

 import string
 
 def get_max_value(text):
     text = text.lower()
     return max(string.ascii_lowercase, key=text.count)

Using the key parameter of max() function, the characters with the most occurrences are skillfully extracted.

 max(arg1, arg2, *args, *[, key=func]) -> value

Let me explain the principle of the code max(string.ascii_lowercase, key=text.count).

string.ascii_lowercase is equivalent to 'abcdefghijklmnopqrstuvwxyz', and the key parameter of max() function is used to filter the maximum value of the return value of the key function. If there are multiple qualified values, select the first one.

'''
No one answers any questions? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded partners to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
max(range(6), key = lambda x : x>2)
>>> 3
# Brought into the key function, each element returns a Boolean value, equivalent to [False, False, False, True, True, True]
# The key function requires the return value to be True. If there are multiple matching values, select the first one.

max([3,5,2,1,4,3,0], key = lambda x : x)
>>> 5
# Brought into the key function, each element returns its own value. The maximum value is 5 and returns 5

max('ah', 'bf', key=lambda x: x[1])
>>> 'ah'
# Bring in the key function, and each string returns the last character. h of 'ah' is greater than f in 'bf', so 'ah' is returned

max('ah', 'bf', key=lambda x: x[0])
>>> 'bf'
# Bring in the key function, and each string returns the first character. b of 'bf' is greater than a in 'ah', so 'bf' is returned

max('abcdefghijklmnopqrstuvwxyz', key=text.count) # text = 'Hello World'
>>> 'l'
# Bring in the key function and return the number of occurrences of each character in 'Hello World'. The character with the most occurrences is' l ', so' l 'is output

In this way, you can understand why you can find the characters that appear the most times and conform to the alphabet through the key parameter of max() function!

Keywords: Python

Added by asmon on Wed, 09 Feb 2022 22:23:06 +0200