python - Manipulate a part of variable while keeping the original intact -
what cleanest way manipulate / use / remove part of variable whilst keeping original variable intact.
lets wanted value in following list corresponds highest numerical value
so example:
max([1, 2, '3a', '10b']) should evaluated as
max([1, 2, 3, 10]) and value '10b' should returned
extended approach using re.search() function:
import re max_item = max([1, 2, '3abc', '10bc'], key=lambda n: int(re.search(r'\d+', str(n)).group(0))) print(max_item) the output:
10bc wiki
Comments
Post a Comment