Home > Software design >  How to extract minimum of multiple numbers from the string of dictionary values [duplicate]
How to extract minimum of multiple numbers from the string of dictionary values [duplicate]

Time:09-17

Need to extract a minimum of all the numbers from the string, i.e 3

sample_dict = {'job_exp': ('3yrs', '5 years', 'Experience range 7-8 Years')}

my solution:

import re
sample_dict["job_exp"] = "".join(str(e) for e in sample_dict["job_exp"])

exp_min = int()
for i in list(map(int, re.findall(r"\d ", sample_dict["job_exp"]))):
    exp_min = i
print(exp_min)

Result: 8

Expected result: minimum of all the numbers, i.e : 3

CodePudding user response:

You don't need a loop.

import re
sample_dict = {'job_exp': ('3yrs', '5 years', 'Experience range 7-8 Years')}

exp_min = min(map(int, re.findall(r"\d ", ''.join(sample_dict["job_exp"]))))
print(exp_min) #3
  • Related