python怎么提取字符串的浮点数_python:从python字符串列表中提取float(AUD 31.99)...

这篇博客介绍了如何使用Python从包含澳元金额的字符串列表中提取浮点数,如['31.40AUD', '32.99AUD', '37.24AUD'],通过map函数和lambda表达式将它们转换为浮点数并求得总和。
摘要由CSDN通过智能技术生成

python: extract float from a python list of string( AUD 31.99).

I used openpyxl to read from an excel file the amount list. and i saved it in a list but the list is in string form like this:

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

I need to get the float from the string item list so that i can later save it in a new list to get the total of them.

Desired output:

[31.40, 32.99, 37.24]

I have already tried these:

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

print(newList)

Output:

[31.40]

But How can I use this for all the item elements?

I am new to python, this is just for some work i do, wanted to see the total using python instead of using excel`s find & replace option.

thanks

解决方案

You can use the map function:

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

output = list(map(lambda elem: float(elem.split()[0]), inList))

print(output)

Output:

[31.4, 32.99, 37.24]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值