如何在Python中对数组的列求和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43459581/
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
How to sum columns of an array in Python
提问by Alexander
How do I add up all of the values of a column in a python array? Ideally I want to do this without importing any additional libraries.
如何将python数组中一列的所有值相加?理想情况下,我想在不导入任何其他库的情况下执行此操作。
input_val = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
output_val = [3, 6, 9, 12, 15]
I know I this can be done in a nested for loop, wondering if there was a better way (like a list comprehension)?
我知道这可以在嵌套的 for 循环中完成,想知道是否有更好的方法(如列表理解)?
回答by Stephen Rauch
zip
and sum
can get that done:
zip
并且sum
可以做到这一点:
Code:
代码:
[sum(x) for x in zip(*input_val)]
zip
takes the contents of the input list and transposes them so that each element of the contained lists is produced at the same time. This allows the sum
to see the first elements of each contained list, then next iteration will get the second element of each list, etc...
zip
获取输入列表的内容并转置它们,以便同时生成包含列表的每个元素。这允许sum
查看每个包含列表的第一个元素,然后下一次迭代将获取每个列表的第二个元素,依此类推...
Test Code:
测试代码:
input_val = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
print([sum(x) for x in zip(*input_val)])
Results:
结果:
[3, 6, 9, 12, 15]
回答by JavNoor
In case you decide to use any library, numpy easily does this:
如果您决定使用任何库,numpy 很容易做到这一点:
np.sum(input_val,axis=0)
np.sum(input_val,axis=0)
回答by Moinuddin Quadri
You may also use sum
with zip
within the map
function:
# In Python 3.x
>>> list(map(sum, zip(*input_val)))
[3, 6, 9, 12, 15]
# explicitly type-cast it to list as map returns generator expression
# In Python 2.x, explicit type-casting to list is not needed as `map` returns list
>>> map(sum, zip(*input_val))
[3, 6, 9, 12, 15]
回答by Alex
This should work:
这应该有效:
[sum(i) for i in zip(*input_val)]
回答by Ajax1234
Try this:
尝试这个:
input_val = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
output_val = [sum([i[b] for i in input_val]) for b in range(len(input_val[0]))]
print output_val
回答by Asav Patel
I think this is the most pythonic way of doing this
我认为这是最pythonic的方法
map(sum, [x for x in zip(*input_val)])
回答by Prune
One-liner using list comprehensions: for each column (length of one row), make a list of all the entries in that column, and sum that list.
使用列表推导式的单行:对于每一列(一行的长度),制作该列中所有条目的列表,然后对该列表求和。
output_val = [sum([input_val[i][j] for i in range(len(input_val))]) \
for j in range(len(input_val[0]))]
回答by LLL
Try this code. This will make output_val
end up as [3, 6, 9, 12, 15]
given your input_val
:
试试这个代码。这将使output_val
最终成为[3, 6, 9, 12, 15]
您的input_val
:
input_val = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
vals_length = len(input_val[0])
output_val = [0] * vals_length # init empty output array with 0's
for i in range(vals_length): # iterate for each index in the inputs
for vals in input_val:
output_val[i] += vals[i] # add to the same index
print(output_val) # [3, 6, 9, 12, 15]
回答by CONvid19
I guess you can use:
我想你可以使用:
import numpy as np
new_list = sum(map(np.array, input_val))
回答by Tom Souza
Using Numpy you can easily solve this issue in one line:
使用 Numpy,您可以在一行中轻松解决此问题:
1: Input
1:输入
input_val = [[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]]
2: Numpy does the math for you
2:Numpy 为你做数学
np.sum(input_val,axis=0)
3: Then finally the results
3:最后是结果
array([ 3, 6, 9, 12, 15])