+ 4

Alphanumeric string

How to find max number occuring in alphanumeric string? Ex:- input ='23dy10jp98' Output=98

22nd Dec 2018, 12:32 PM
Devendra Yadav
Devendra Yadav - avatar
2 odpowiedzi
+ 12
I'd use regular expressions. import re s = '23dy10jp98' pattern = re.compile(r'\d+') print(max([int(n) for n in pattern.findall(s)])) # output: 98
22nd Dec 2018, 12:46 PM
Anna
Anna - avatar
+ 6
A way without re: s = '23dy10jp98' s = ''.join([d if d.isdigit() else ' ' for d in s ]) print(max([int(n) for n in s.split()]))
22nd Dec 2018, 1:17 PM
HonFu
HonFu - avatar