gpt4 book ai didi

python:从 python 字符串列表中提取 float (31.99 澳元)

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:13 27 4
gpt4 key购买 nike

python:从字符串的 python 列表中提取 float (31.99 澳元)。我使用 openpyxl 从 excel 文件中读取金额列表。我将它保存在一个列表中,但该列表是这样的字符串形式:

['31.40 AUD', ' 32.99 AUD', '37.24 AUD']

我需要从字符串项列表中获取 float ,以便稍后可以将其保存在新列表中以获取它们的总数。

期望的输出:

[31.40, 32.99, 37.24]

我已经尝试过这些:

newList = re.findall("\d+\.\d+", tot[0])
print(newList)

输出:

[31.40]

但是我怎样才能将它用于所有项目元素呢?

我是 python 的新手,这只是我做的一些工作,想查看使用 python 而不是使用 excel 的查找和替换选项的总数。谢谢

最佳答案

您可以使用 map功能:

inList = ['31.40 AUD', ' 32.99 AUD', '37.24 AUD']
output = list(map(lambda elem: float(elem.split()[0]), inList))
print(output)

输出:

[31.4, 32.99, 37.24]

关于python:从 python 字符串列表中提取 float (31.99 澳元),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53276670/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com