获取字符串中出现次数最多的所有数字
get all numbers with the most frequent digits length in a string
我花了几个小时寻找答案,但不幸的是在任何地方都找不到适合我问题的答案。
假设我有一个看起来像这样的字符串 -
str_int = "I'm 35, and I have 2 kids ages 10 and 12"
我想从这些字符串中导出所有具有最常见长度的数字。所以满意的结果将是:
[35,10,12]
请注意,数字 2 不在数组中,因为我只想要长度最频繁的数字。
我也想将其应用于浮点数。所以如果我有以下字符串:
str_float = "I'm 35.055, and I have 2.100 kids ages 10.0 and 12.505"
预期结果将是:
[35.055, 12.505]
注意这2个数字是5位数字,另外两个数字分别是4位和3位数字,所以我想要最常见的数字长度数字,也就是5位数字。
希望它有意义,谢谢!
您可以使用 re.findall
提取数字,使用 collections.Counter
找到最频繁的长度,然后列出具有此长度的数字:
import re
from collections import Counter
def most_freq(s):
lst = [(x, len(x) - ('.' in x)) for x in re.findall(r"\d+(?:\.\d+)?", s)]
frequent_length, _ = Counter(x[1] for x in lst).most_common(1)[0]
return [x[0] for x in lst if x[1] == frequent_length]
str_int = "I'm 35, and I have 2 kids ages 10 and 12"
str_float = "I'm 35.055, and I have 2.100 kids ages 10.0 and 12.505"
str_another = "0.017 0.019 3 1 0.020 2 0.024 3 0.032 0.0157"
print(most_freq(str_int)) # ['35', '10', '12']
print(most_freq(str_float)) # ['35.055', '12.505']
print(most_freq(str_another)) # ['0.017', '0.019', '0.020', '0.024', '0.032']
我花了几个小时寻找答案,但不幸的是在任何地方都找不到适合我问题的答案。
假设我有一个看起来像这样的字符串 -
str_int = "I'm 35, and I have 2 kids ages 10 and 12"
我想从这些字符串中导出所有具有最常见长度的数字。所以满意的结果将是:
[35,10,12]
请注意,数字 2 不在数组中,因为我只想要长度最频繁的数字。
我也想将其应用于浮点数。所以如果我有以下字符串:
str_float = "I'm 35.055, and I have 2.100 kids ages 10.0 and 12.505"
预期结果将是:
[35.055, 12.505]
注意这2个数字是5位数字,另外两个数字分别是4位和3位数字,所以我想要最常见的数字长度数字,也就是5位数字。
希望它有意义,谢谢!
您可以使用 re.findall
提取数字,使用 collections.Counter
找到最频繁的长度,然后列出具有此长度的数字:
import re
from collections import Counter
def most_freq(s):
lst = [(x, len(x) - ('.' in x)) for x in re.findall(r"\d+(?:\.\d+)?", s)]
frequent_length, _ = Counter(x[1] for x in lst).most_common(1)[0]
return [x[0] for x in lst if x[1] == frequent_length]
str_int = "I'm 35, and I have 2 kids ages 10 and 12"
str_float = "I'm 35.055, and I have 2.100 kids ages 10.0 and 12.505"
str_another = "0.017 0.019 3 1 0.020 2 0.024 3 0.032 0.0157"
print(most_freq(str_int)) # ['35', '10', '12']
print(most_freq(str_float)) # ['35.055', '12.505']
print(most_freq(str_another)) # ['0.017', '0.019', '0.020', '0.024', '0.032']