# 将两个LISTS的值之和添加到新的LIST中

## 问题：将两个LISTS的值之和添加到新的LIST中

first = [1,2,3,4,5]
second = [6,7,8,9,10]

third = [7,9,11,13,15]

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]

## 回答 0

zip功能在此处有用，可与列表推导一起使用。

[x + y for x, y in zip(first, second)]

lists_of_lists = [[1, 2, 3], [4, 5, 6]]
[sum(x) for x in zip(*lists_of_lists)]
# -> [5, 7, 9]

The zip function is useful here, used with a list comprehension.

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

import operator

From docs

import operator

## 回答 2

Python 2.x和3.x：

[a[i]+b[i] for i in range(len(a))]

Assuming both lists a and b have same length, you do not need zip, numpy or anything else.

Python 2.x and 3.x:

[a[i]+b[i] for i in range(len(a))]

## 回答 3

numpy中的默认行为是逐组件添加

import numpy as np

array([7,9,11,13,15])

Default behavior in numpy is add componentwise

import numpy as np

which outputs

array([7,9,11,13,15])

## 回答 4

[sum(sublist) for sublist in itertools.izip(*myListOfLists)]

This extends itself to any number of lists:

[sum(sublist) for sublist in itertools.izip(*myListOfLists)]

In your case, myListOfLists would be [first, second]

## 回答 5

first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map(sum, zip(first, second))

Try the following code:

first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map(sum, zip(first, second))

## 回答 6

three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]

from numpy import sum
three = sum([first,second], axis=0) # array([7,9,11,13,15])

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])

## 回答 7

first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
three = map(lambda x,y: x+y,first,second)
print three

Output
[7, 9, 11, 13, 15]
first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
three = map(lambda x,y: x+y,first,second)
print three

Output
[7, 9, 11, 13, 15]

## 回答 8

list(map(lambda x,y: x+y, a,b))

one-liner solution

list(map(lambda x,y: x+y, a,b))

## 回答 9

Thiru在3月17日9:25回答了我的问题。

three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]

from numpy import sum
three = sum([first,second], axis=0) # array([7,9,11,13,15])

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]

My answer is repeated with Thiru's that answered it in Mar 17 at 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])

You need numpy!

numpy array could do some operation like vectors
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]

## 回答 10

def sum_lists(*args):
return list(map(sum, zip(*args)))

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

sum_lists(a,b)

[2, 4, 6]

sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])

[19, 19, 19, 19, 19]

If you have an unknown number of lists of the same length, you can use the below function.

Here the *args accepts a variable number of list arguments (but only sums the same number of elements in each). The * is used again to unpack the elements in each of the lists.

def sum_lists(*args):
return list(map(sum, zip(*args)))

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

sum_lists(a,b)

Output:

[2, 4, 6]

Or with 3 lists

sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])

Output:

[19, 19, 19, 19, 19]

## 回答 11

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

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:

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

## 回答 12

class SumList(object):
def __init__(self, this_list):
self.mylist = this_list

new_list = []
zipped_list = zip(self.mylist, other.mylist)
for item in zipped_list:
new_list.append(item[0] + item[1])
return SumList(new_list)

def __repr__(self):
return str(self.mylist)

list1 = SumList([1,2,3,4,5])
list2 = SumList([10,20,30,40,50])
sum_list1_list2 = list1 + list2
print(sum_list1_list2)

[11, 22, 33, 44, 55]

Here is another way to do it. We make use of the internal __add__ function of python:

class SumList(object):
def __init__(self, this_list):
self.mylist = this_list

new_list = []
zipped_list = zip(self.mylist, other.mylist)
for item in zipped_list:
new_list.append(item[0] + item[1])
return SumList(new_list)

def __repr__(self):
return str(self.mylist)

list1 = SumList([1,2,3,4,5])
list2 = SumList([10,20,30,40,50])
sum_list1_list2 = list1 + list2
print(sum_list1_list2)

Output

[11, 22, 33, 44, 55]

## 回答 13

sum = [x + y for x, y in zip(v1, v2)]
if not len(v1) >= len(v2):
sum += v2[len(v1):]
else:
sum += v1[len(v2):]

return sum

#for testing
if __name__=='__main__':
a = [1, 2]
b = [1, 2, 3, 4]
print(a)
print(b)

If you want to add also the rest of the values in the lists you can use this (this is working in Python3.5)

sum = [x + y for x, y in zip(v1, v2)]
if not len(v1) >= len(v2):
sum += v2[len(v1):]
else:
sum += v1[len(v2):]

return sum

#for testing
if __name__=='__main__':
a = [1, 2]
b = [1, 2, 3, 4]
print(a)
print(b)

## 回答 14

first = [1,2,3,4,5]
second = [6,7,8,9,10]
#one way
third = [x + y for x, y in zip(first, second)]
print("third" , third)
#otherway
fourth = []
for i,j in zip(first,second):
global fourth
fourth.append(i + j)
print("fourth" , fourth )
#third [7, 9, 11, 13, 15]
#fourth [7, 9, 11, 13, 15]
first = [1,2,3,4,5]
second = [6,7,8,9,10]
#one way
third = [x + y for x, y in zip(first, second)]
print("third" , third)
#otherway
fourth = []
for i,j in zip(first,second):
global fourth
fourth.append(i + j)
print("fourth" , fourth )
#third [7, 9, 11, 13, 15]
#fourth [7, 9, 11, 13, 15]

## 回答 15

N=int(input())
num1 = list(map(int, input().split()))
num2 = list(map(int, input().split()))
sum=[]

for i in range(0,N):
sum.append(num1[i]+num2[i])

for element in sum:
print(element, end=" ")

print("")

Here is another way to do it.It is working fine for me .

N=int(input())
num1 = list(map(int, input().split()))
num2 = list(map(int, input().split()))
sum=[]

for i in range(0,N):
sum.append(num1[i]+num2[i])

for element in sum:
print(element, end=" ")

print("")

## 回答 16

j = min(len(l1), len(l2))
l3 = [l1[i]+l2[i] for i in range(j)]
j = min(len(l1), len(l2))
l3 = [l1[i]+l2[i] for i in range(j)]

## 回答 17

first = [1,2,3,4,5]
second = [6,7,8,9,10]
three=[]

for i in range(0,5):
three.append(first[i]+second[i])

print(three)

Perhaps the simplest approach:

first = [1,2,3,4,5]
second = [6,7,8,9,10]
three=[]

for i in range(0,5):
three.append(first[i]+second[i])

print(three)

## 回答 18

import numpy as np

third = np.array(first) + np.array(second)

print third

[7, 9, 11, 13, 15]

If you consider your lists as numpy array, then you need to easily sum them:

import numpy as np

third = np.array(first) + np.array(second)

print third

[7, 9, 11, 13, 15]

## 回答 19

from itertools import zip_longest  # izip_longest for python2.x

l1 = [1, 2, 3]
l2 = [4, 5, 6, 7]

>>> list(map(sum, zip_longest(l1, l2, fillvalue=0)))
[5, 7, 9, 7]

What if you have list with different length, then you can try something like this (using zip_longest)

from itertools import zip_longest  # izip_longest for python2.x

l1 = [1, 2, 3]
l2 = [4, 5, 6, 7]

>>> list(map(sum, zip_longest(l1, l2, fillvalue=0)))
[5, 7, 9, 7]

## 回答 20

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

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