简体   繁体   English

汇总列表中的值(python)

[英]Summing the values in a list (python)

Hi all I am having trouble trying to sum all the values in a list. 大家好,我在尝试对列表中的所有值求和时遇到麻烦。 I'm not sure if this is actually possible first. 我不确定这是否首先可能。

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

This code is what I have so far and produces the result 这段代码是我到目前为止所拥有的,并产生结果

['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']

The above list is not typed but produced by the function code. 上面的列表没有键入,而是由功能代码生成的。 Does this affect the result? 这会影响结果吗?

Can anyone please assist me in showing me how to solve this problem and sum all these values in a code from this list. 任何人都可以帮助我向我展示如何解决此问题,并将所有这些值加到此列表中的代码中。 I believe I will have to join string to do so but am unsure. 我相信我必须加入字符串,但是不确定。 Can you please inform me whether this is possible to sum all these values of the list and if so how can I do it? 您能否告诉我是否可以对列表的所有这些值求和,如果可以,我该怎么做?

EDIT: 编辑:

THANKS FOR ALL THE HELP GUYS 

I would use math.fsum for a more accurate sum: 我会使用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

As opposed to 相对于

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

Using your code, a more efficient way of doing it without saving the whole file into memory at once: 使用您的代码,无需立即将整个文件保存到内存中的更有效方式:

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)

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

sum(float(x) for x in a if x)

这也适用:

reduce(lambda x,y:x+y, map(float, filter(lambda x:x!='', a)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM