Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字符串中提取某些整数,然后对其进行规格化_Python - Fatal编程技术网

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

Python 从字符串中提取某些整数,然后对其进行规格化,python,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.isdig

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

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]

使用
re.findall

import re
res = []
for s in string:
    res.extend(re.findall('\d+', s))
res = [int(r)/100 for r in res]
输出:

[0.45, 0.6]

非常感谢你,先生。这真的很有帮助!谢谢,我需要等待4分钟的冷却时间才能将其标记为答案顺便问一下,先生,有没有办法将%也包括在列表中?我会删除它。因为有时候,我们的句子不太清楚,可能是“80people”,那么这将是非常错误的,对吗?@kingkongmanoftheworldinchaos那么您希望您的输出像
['0.45%','0.6%']
?在这种情况下,将最后一行更改为
res=['{}%'。res中的r的格式(int(r)/100)