gpt4 book ai didi

python - 字符串数组数据需要去掉美元符号并转换为 float

转载 作者:行者123 更新时间:2023-11-30 22:52:54 26 4
gpt4 key购买 nike

我有数据,例如:

['$15.50']
['$10.00']
['$15.50']
['$15.50']
['$22.28']
['$50']
['$15.50']
['$10.00']

我想去掉美元符号并将字符串转换为 float ,这样我就可以使用这些数字进行多次计算。我已经尝试过以下方法:

array[0] = float(array.text.strip('$')) 

这给了我一个属性错误,因为显然“列表”对象没有“文本”属性。我的错。是否有类似的方法来删除“列表”对象?任何其他建议也将受到欢迎。提前致谢。

最佳答案

尝试使用 list comprehension :

array = [float(x.strip("$")) for x in array]

关于python - 字符串数组数据需要去掉美元符号并转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38377328/

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