程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python exercise: calculate the characters that appear most times in the list (three methods)

編輯:Python

Here's a text with different letters and punctuation , You need to find the letters that appear the most , The letters returned must be lowercase , When checking the most desired letters , Case insensitive , So in your search “A” == “a”. Make sure you don't calculate punctuation , Numbers and spaces , Just count the letters .

If you find it Two or more letters of the same frequency , Return the first letter in the alphabet . for example – “one” contain “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 letters appear in a text . for example : If we know which letters appear at a certain frequency , We can easily crack a simple addition or replacement password . It's an interesting thing for language experts !

Premise : Password contains only ASCII Code symbols 0 < len(text) ≤ 105

Common method :

utilize collections Tools Counter, Sort the frequency of elements in the list .Counter The return value is in descending order of the frequency of the elements Counter object , It's a subclass of dictionaries , So you can use the dictionary method .

import re
from collections import Counter
def get_max_value(text):
text = text.lower()
result = re.findall('[a-zA-Z]', text) # Remove the 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]

Reduction method :

Also use Counter, But through the list derivation , Can make the function more refined . That's 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]

The best way :

I have to praise this method , It's really crisp . It's a clever use of max() function .

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :153708845 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
import string
def get_max_value(text):
text = text.lower()
return max(string.ascii_lowercase, key=text.count)

Take advantage of max() Functional key Parameters , Skillfully extract the most frequent characters .

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

Let me explain to you max(string.ascii_lowercase, key=text.count) The principle of this code .

string.ascii_lowercase Equivalent to ‘abcdefghijklmnopqrstuvwxyz’ and max() function key The function of the parameter is : The screening is in line with key The maximum value of the return value of the function , If there are multiple values that match the criteria , Select the first one .

max(range(6), key = lambda x : x>2)
>>> 3
# Into the key Function , Each element returns a Boolean value , amount to [False, False, False, True, True, True]
# key The function requires the return value to be True, There are multiple values that match , Choose the first one .
max([3,5,2,1,4,3,0], key = lambda x : x)
>>> 5
# Into the key Function , Each element returns its own value , The maximum value is 5, return 5.
max('ah', 'bf', key=lambda x: x[1])
>>> 'ah'
# Into the key function , Each string returns the last character , among 'ah' Of h Be greater than 'bf' Medium f, Therefore return 'ah'
max('ah', 'bf', key=lambda x: x[0])
>>> 'bf'
# Into the key function , Each string returns the first character , among 'bf' Of b Be greater than 'ah' Medium a, Therefore return 'bf'
max('abcdefghijklmnopqrstuvwxyz', key=text.count) # text = 'Hello World'
>>> 'l'
# Into the key function , Return each character in 'Hello World' Is the number of times , The most frequent character is 'l', So output 'l'

In this way, we can understand why max() Functional key Parameters can be obtained, the number of occurrences is the most , And match the alphabetic characters


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved