Home > Net >  get all numbers with the most frequent digits length in a string
get all numbers with the most frequent digits length in a string

Time:11-04

I'm looking for an answer for a few hours but unfortunately can not find one that suits my problem anywhere.

Suppose I have a certain string that looks like this -

str_int = "I'm 35, and I have 2 kids ages 10 and 12"

I want to export from these str all the numbers with the most common length. So the satisfy result will be:

[35,10,12] 

Note that the number 2 is not in the array since I only want numbers with the most frequent length.

Also I want to apply this to float numbers as well. so if I have the following string:

str_float = "I'm 35.055, and I have 2.100 kids ages 10.0 and 12.505"

the expected result will be:

[35.055, 12.505]

Note that these 2 numbers are 5 digits numbers and the other two numbers are 4 and 3 digits respectively, so I want the most common digits length numbers which is the numbers with the 5 digits.

Hope it makes sense , thanks !

CodePudding user response:

You can extract the numbers using re.findall, find the most frequent length using collections.Counter, and then list the numbers that have this length:

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']
  • Related