Python 如何对列表的一列求和?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15352457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:53:13  来源:igfitidea点击:

How can I sum a column of a list?

pythonfor-loop

提问by MarJamRob

I have a Python array, like so:

我有一个 Python 数组,如下所示:

[[1,2,3],
 [1,2,3]]

I can add the row by doing sum(array[i]), how can I sum a column, using a double for loop?

我可以通过执行添加行sum(array[i]),如何使用双循环对列求和?

I.E. for the first column, I could get 2, then 4, then 6.

IE 对于第一列,我可以得到 2,然后是 4,然后是 6。

采纳答案by martineau

Using a forloop (in a generator expression):

使用for循环(在生成器表达式中):

data = [[1,2,3],
        [1,2,3]]

column = 1
print(sum(row[column] for row in data))  # -> 4

回答by Gareth Latty

You don't need a loop, use zip()to transpose the list, then take the desired column:

您不需要循环,用于zip()转置列表,然后获取所需的列:

sum(list(zip(*data)[i]))

(Note in 2.x, zip()returns a list, so you don't need the list()call).

(注意在 2.x 中,zip()返回一个列表,因此您不需要list()调用)。

Edit: The simplest solution to this problem, without using zip(), would probably be:

编辑:这个问题最简单的解决方案,不使用zip(),可能是:

column_sum = 0
for row in data:
    column_sum += row[i]

We just loop through the rows, taking the element and adding it to our total.

我们只是遍历行,获取元素并将其添加到我们的总数中。

This is, however, less efficient and rather pointless given we have built-in functions to do this for us. In general, use zip().

然而,鉴于我们有内置函数为我们执行此操作,因此效率较低且毫无意义。一般情况下,使用zip().

回答by Ashwini Chaudhary

you can use zip():

你可以使用zip()

In [16]: lis=[[1,2,3],
   ....:  [1,2,3]]

In [17]: map(sum,zip(*lis))
Out[17]: [2, 4, 6]

or with a simple for loops:

或使用简单的 for 循环:

In [25]: for i in xrange(len(lis[0])):
    summ=0
    for x in lis:
        summ+=x[i]
    print summ
   ....:     
2
4
6

回答by Mark Ransom

[sum(row[i] for row in array) for i in range(len(array[0]))]

That should do it. len(array[0])is the number of columns, so iiterates through those. The generator expression row[i] for row in arraygoes through all of the rows and selects a single column, for each column number.

那应该这样做。len(array[0])是列数,因此i遍历这些列。生成器表达式row[i] for row in array遍历所有行并为每个列号选择一列。

回答by Artsiom Rudzenka

Try this:

尝试这个:

a = [[1,2,3],
     [1,2,3]]

print [sum(x) for x in zip(*a)]

zip function description

zip功能说明

回答by monkut

You may be interested in numpy, which has more advanced array features. One of which is to easily sum a column:

您可能对numpy感兴趣,它具有更高级的数组功能。其中之一是轻松地对一列求和:

from numpy import array

a = array([[1,2,3],
 [1,2,3]])

column_idx = 1
a[:, column_idx].sum() # ":" here refers to the whole array, no filtering.

回答by icehand

I think the easiest way is this:

我认为最简单的方法是这样的:

sumcolumn=data.sum(axis=0)

print (sumcolumn)

回答by Arunim sharma

You can use numpy:

您可以使用 numpy:

import numpy as np
a = np.array([[1,2,3],[1,2,3]])
a.sum(0)