Using Python 3, I have a list of the form
ls = ["a", "b_0", "b_1", "b_2", "c", "d_0", "d_1"]
I have to find out the maximum number after "b_". In this case, the desired output is 2.
I came up with something like that:
import re
p = re.compile("b_(\d+)")
nums = [int(p.match(l).group(1)) for l in ls if p.match(l) is not None]
res_max = max(nums)
But I don't like the repetition of p.match two times.
What is the most pythonic way of achieving this?
The maximum number after d_ is not guaranteed to be less than the maximum number after b_
I specifically need to find out the maximum number after the "b_" prefix. I am not interested in the maximum number after any other prefixes
"a"? Or do you mean to just ignore anything that has no"b_"in the prefix?b_as a prefix