owc*_*wca 0 python string replace list
我有一个像这样的字符串"1 1 3 2 1 1 1 2 1 1 1 1 1 1 1 1,5 0,33 0,66 1 0,33 0,66 1 1 2 1 1 2 1 1 2 0,5 0,66 2 1 2 1 1 1 0 1".
如何在python中相互添加元素?
我试过了 :
list = []
for x in str.replace(' ', ''):
list.append(x)
sum = 0
for y in list:
sum = sum + double(x)
Run Code Online (Sandbox Code Playgroud)
但我经常遇到错误.
print sum(float(x.replace(',', '.')) for x in str.split(' '))
Run Code Online (Sandbox Code Playgroud)
输出:
45.64
Run Code Online (Sandbox Code Playgroud)