从字符串中提取某些整数,然后对i进行规格化

2024-10-01 17:33:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望规范化字符串中的整数。例如:

string = ["There is a discount of 45% on the sales in the shopping mall", "60percent discount will be given to any purchase made"]

我想知道这是否可能做到

a = []
for x in string:
    xsplit = x.split()
    for xx in xsplit:
        if xx.isdigit():
            newxx = xx/100
            a.append(newxx)

我上面的代码非常昂贵,循环太多。我希望找到一种方法来实现我的预期输出,同时保持更短的代码。这可能吗?我会在这里不断更新我的新测试代码。请帮助我

我收到错误:

unsupported operand type(s) for /: 'str' and 'int'

我的预期输出应该是:

[0.45, 0.6]

Tags: ofthe字符串代码inforstringis

热门问题