Python 将两个 LISTS 的值的总和添加到新 LIST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14050824/
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
Add SUM of values of two LISTS into new LIST
提问by pygaur
I have the following two lists:
我有以下两个列表:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
Now I want to add the items from both of these lists into a new list.
现在我想将这两个列表中的项目添加到一个新列表中。
output should be
输出应该是
third = [7,9,11,13,15]
采纳答案by tom
The zipfunction is useful here, used with a list comprehension.
该zip函数在这里很有用,与列表理解一起使用。
[x + y for x, y in zip(first, second)]
If you have a list of lists (instead of just two lists):
如果您有一个列表列表(而不仅仅是两个列表):
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
[sum(x) for x in zip(*lists_of_lists)]
# -> [5, 7, 9]
回答by cdhowie
You can use zip(), which will "interleave" the two arrays together, and then map(), which will apply a function to each element in an iterable:
您可以使用zip(),它将两个数组“交错”在一起,然后使用map(),它将对可迭代对象中的每个元素应用一个函数:
>>> a = [1,2,3,4,5]
>>> b = [6,7,8,9,10]
>>> zip(a, b)
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
>>> map(lambda x: x[0] + x[1], zip(a, b))
[7, 9, 11, 13, 15]
回答by inspectorG4dget
This extends itself to any number of lists:
这将自身扩展到任意数量的列表:
[sum(sublist) for sublist in itertools.izip(*myListOfLists)]
In your case, myListOfListswould be [first, second]
在你的情况下,myListOfLists将是[first, second]
回答by HelloUni
You can use this method but it will work only if both the list are of the same size:
您可以使用此方法,但只有当两个列表的大小相同时它才有效:
first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
third = []
a = len(first)
b = int(0)
while True:
x = first[b]
y = second[b]
ans = x + y
third.append(ans)
b = b + 1
if b == a:
break
print third
回答by Thiru
The easy way and fast way to do this is:
做到这一点的简单方法和快速方法是:
three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]
Alternatively, you can use numpy sum:
或者,您可以使用 numpy sum:
from numpy import sum
three = sum([first,second], axis=0) # array([7,9,11,13,15])
回答by Piece
My answer is repeated with Thiru's that answered it in Mar 17 at 9:25.
我的回答与 Thiru's 重复,他们在 3 月 17 日 9:25 回答了这个问题。
It was simpler and quicker, here are his solutions:
它更简单,更快捷,这是他的解决方案:
The easy way and fast way to do this is:
three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]Alternatively, you can use numpy sum:
from numpy import sum three = sum([first,second], axis=0) # array([7,9,11,13,15])
做到这一点的简单方法和快速方法是:
three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]或者,您可以使用 numpy sum:
from numpy import sum three = sum([first,second], axis=0) # array([7,9,11,13,15])
You need numpy!
你需要麻木!
numpy 数组可以做一些像向量一样的操作import numpy as np
a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = list(np.array(a) + np.array(b))
print c
# [7, 9, 11, 13, 15]
回答by user3582790
Default behavior in numpy is add componentwise
numpy 中的默认行为是按组件添加
import numpy as np
np.add(first, second)
which outputs
哪个输出
array([7,9,11,13,15])
回答by math
Assuming both lists aand bhave same length, you do not need zip, numpy or anything else.
假设两个列表a,并b具有相同的长度,你不需要压缩,numpy的或其他任何东西。
Python 2.x and 3.x:
Python 2.x 和 3.x:
[a[i]+b[i] for i in range(len(a))]
回答by Maciej Puczkowski
Try the following code:
试试下面的代码:
first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map(sum, zip(first, second))

