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

标签 python arrays string

我有数据,例如:

['$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/

相关文章:

python - 如何在更改窗口大小时更改窗口的布局(tkinter,python)?

python - 在传递给 apply() 的自定义函数中访问先前计算的结果

java - 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 以及如何防止它?

arrays - Excel中计算两个数组相减的结果是否超过某个值

将 char 数组元素与字符串文字进行比较

python - matplotlib/Python 中条形图的单独标记条

Python 正则表达式 : replace numbers and special characters except years

JavaScript For 循环在代码中的某一行之后不起作用

Python 错误 : TypeError: a bytes-like object is required, 不是 'str'

c++ - 为什么函数立即完成一次循环,然后再次运行?