1

大家好,我在尝试对列表中的所有值求和时遇到问题。我不确定这是否真的可行。

dL= list(csv.reader(input))
sL = dL [14247:14611]
a = [row[5] for row in sL[1:]] 
print a

这段代码是我到目前为止所拥有的并产生结果

['5.0', '0.0', '0.0', '0.0', '0.0', '1.6', '4.4', '14.0', '0.0', '0.0', '0.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '95.6', '63.6', '0.0', '0.0', '0.0', '9.0', '0.0', '0.0', '0.0', '0.0', '20.2', '14.4', '1.6', '0.0', '7.6', '2.4', '0.0', '11.4', '0.0', '79.4', '60.4', '27.2', '', '1.8', '0.0', '0.0', '29.6', '67.6', '2.2', '0.0', '0.0', '0.0', '14.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '', '3.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '6.2', '0.0', '0.0', '0.0', '0.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '11.8', '2.4', '0.0', '0.0', '0.0', '0.4', '16.8', '8.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '2.6', '27.2', '77.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '9.2', '2.6', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '1.6', '0.0', '0.0', '0.0', '0.0', '0.0', '6.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '', '', '', '', '', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '37.8', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0']

上面的列表不是输入的,而是由功能代码产生的。这会影响结果吗?

谁能帮助我向我展示如何解决此问题并将所有这些值汇总到此列表中的代码中。我相信我必须加入字符串才能这样做,但我不确定。您能否告诉我这是否可以将列表的所有这些值相加,如果可以,我该怎么做?

编辑:

THANKS FOR ALL THE HELP GUYS 
4

3 回答 3

5

我会使用math.fsum更准确的总和:

>>> nums = ['5.0', '0.0', '0.0', '0.0', '0.0', '1.6', '4.4', '14.0', '0.0', '0.0', '0.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '95.6', '63.6', '0.0', '0.0', '0.0', '9.0', '0.0', '0.0', '0.0', '0.0', '20.2', '14.4', '1.6', '0.0', '7.6', '2.4', '0.0', '11.4', '0.0', '79.4', '60.4', '27.2', '', '1.8', '0.0', '0.0', '29.6', '67.6', '2.2', '0.0', '0.0', '0.0', '14.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '', '3.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '6.2', '0.0', '0.0', '0.0', '0.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '11.8', '2.4', '0.0', '0.0', '0.0', '0.4', '16.8', '8.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '2.6', '27.2', '77.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '9.2', '2.6', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '0.2', '0.0', '0.0', '0.0', '0.0', '1.6', '0.0', '0.0', '0.0', '0.0', '0.0', '6.8', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '', '', '', '', '', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '37.8', '0.2', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0']
>>> from math import fsum
>>> fsum(float(n or 0.0) for n in nums)
749.8

相对于

>>> sum(float(n or 0.0) for n in nums)
749.8000000000003

使用您的代码,这是一种更有效的方法,无需一次将整个文件保存到内存中:

from itertools import islice
dL= csv.reader(input)
sL = islice(dL, 14247+1, 14611)    
a = [row[5] for row in sL] 
print fsum(float(n or 0.0) for n in a)
于 2013-05-27T09:26:03.700 回答
2

您可以将所有数字相加,sum但需要先将字符串转换为浮点数并检查空字符串:

sum(float(x) for x in a if x)
于 2013-05-27T09:27:47.550 回答
0

这也有效:

reduce(lambda x,y:x+y, map(float, filter(lambda x:x!='', a)))
于 2013-05-27T09:42:07.587 回答