python:从python字符串列表中提取float(AUD 31.99) [英] python: extract float from a python list of string( AUD 31.99)

查看:538
本文介绍了python:从python字符串列表中提取float(AUD 31.99)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python:从python字符串列表中提取float(AUD 31.99). 我使用openpyxl从excel文件中读取金额列表.并且我将其保存在列表中,但是列表是这样的字符串形式:

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.

所需的输出:

[31.40, 32.99, 37.24]

我已经尝试过这些:

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

输出:

[31.40]

但是如何将其用于所有项目元素?

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

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

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)

输出:

[31.4, 32.99, 37.24]

这篇关于python:从python字符串列表中提取float(AUD 31.99)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆