Python поиск и замена числа в строке на измененное арифметически

Имеется строка необходима в ней заменить числа на 10% большие от исходных
Можно заменить все символы на определенные но как выполнить арифметику?

a = "_u1_v12 10 яблоко 20 вес32вес23вес33и 78"

print (re.sub('\d', '4', a))

Ответы (1 шт):

Автор решения: Михаил Муругов

re.sub(pattern, repl, string, count=0, flags=0)

repl can be a string or a function

If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string

Отвечая на вопрос: "как выполнить арифметику?":

import re


def increase_by_10_perc(m: re.Match) -> str:
    # Do whatever you want with re.Match obj and return a string
    d = int(m[0]) * 1.1
    return f'{d:.2f}'


a = "_u1_v12 10 яблоко 20 вес32вес23вес33и 78"

print(re.sub(r'\d+', increase_by_10_perc, a))
→ Ссылка