我的问题:我想添加此字符串 '1.14,2.14,3.14,4.14' 中的所有数字,但逗号导致我的求和函数无法正常工作。
我认为使用 strip 函数可以解决我的问题,但似乎还有一些我遗漏或不太理解的东西。
total = 0
for c in '1.14,2.14,3.14'.strip(","):
total = total + float(c)
print total
我搜索了如何从字符串中删除逗号,但我只找到了有关如何从字符串开头或结尾删除逗号的信息。
附加信息:Python 2.7
请您参考如下方法:
我会使用以下内容:
# Get an array of numbers
numbers = map(float, '1,2,3,4'.split(','))
# Now get the sum
total = sum(numbers)
