在Python中从列表中删除所有逗号

在Python中从列表中删除所有逗号,python,Python,我的问题:我想添加此字符串中的所有数字'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我将使用以下内

我的问题:我想添加此字符串中的所有数字
'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)

您不需要
剥离
,您需要
剥离


split
函数将使用您传递给它的分隔符将字符串分隔成一个数组,在您的情况下
split(',')
这将在问题的第一个字符串中添加所有数字:

sum(float(x) for x in '1.14,2.14,3.14,4.14' if x.isdigit())

因为在您的浮点输入列表中似乎有一个模式,所以这一行程序生成它:

>>> sum(map(float, ','.join(map(lambda x:str(x+0.14), range(1,5))).split(',')))
10.559999999999999
由于用逗号连接并立即用逗号拆分没有多大意义,这里有一段更合理的代码:

>>> sum(map(float, map(lambda x:str(x+0.14), range(1,5))))
10.559999999999999


如果您实际上是想求个位数的和,而不是实际的浮点数(尽管我对此表示怀疑,因为您在示例代码中强制转换为浮点):


您需要
split
而不是
strip

>>> for c in '1,2,3,4,5,6,7,8,9'.split(","):
    print float(c)

1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
或者,如果您想要一个:

为了得到总数

>>> sum(map(float, '1,2,3,4,5,6,7,8,9'.split(",")))
45.0
其值=1,2,3,4,5


结果是['1'、'2'、'3'、'4'、'5']

使用此选项删除逗号和空格

a = [1,2,3,4,5]
print(*a, sep = "")
输出:-
12345从
列表中删除逗号

已为Python3更新:

a=[1,2,3,4,5]
b=''.join(str(a).split(','))
从列表中删除逗号,即

[1,2,3,4,5]-->[1,2,3,4,5]

可能稍微不那么优雅,
ast.literal\u eval
也会处理该字符串…从第一个字符串开始,您是希望回答
10.56
还是回答
30.0
>>> sum(map(float, '1,2,3,4,5,6,7,8,9'.split(",")))
45.0
values=input()         
l=values.split(",")   
print(l)
a = [1,2,3,4,5]
print(*a, sep = "")