我有以下两个列表:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
现在,我想从两个列表的项目添加到一个新的列表。
输出应该是
third = [7,9,11,13,15]
我有以下两个列表:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
现在,我想从两个列表的项目添加到一个新的列表。
输出应该是
third = [7,9,11,13,15]
该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]
从文档
import operator
list(map(operator.add, first,second))
假设两个列表a
和b
具有相同的长度,你不需要压缩,numpy的或其他任何东西。
的Python 2.x和3.x:
[a[i]+b[i] for i in range(len(a))]
在numpy的默认行为是分量地添加
import numpy as np
np.add(first, second)
其输出
array([7,9,11,13,15])
这本身扩展到任意数量的列表:
[sum(sublist) for sublist in itertools.izip(*myListOfLists)]
在你的情况, myListOfLists
会[first, second]
试试下面的代码:
first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map(sum, zip(first, second))
最简单的方式,快捷的方式做到这一点是:
three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]
另外,您也可以使用numpy的总和:
from numpy import sum
three = sum([first,second], axis=0) # array([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]
这里是另一种方式来做到这一点。 我们利用蟒蛇的内部__add__功能:
class SumList(object):
def __init__(self, this_list):
self.mylist = this_list
def __add__(self, other):
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]
也许最简单的方法:
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)
可以使用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]
我的回答是重复瑟尔的是回答了3月17日9:25时。
这是简单快捷,这里有他的解决方案:
最简单的方式,快捷的方式做到这一点是:
three = [sum(i) for i in zip(first,second)] # [7,9,11,13,15]
另外,您也可以使用numpy的总和:
from numpy import sum three = sum([first,second], axis=0) # array([7,9,11,13,15])
你需要NumPy的!
numpy array could do some operation like vectorsimport 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]
如果你也想加入其余值在列表中你可以使用这个(这是工作在Python3.5)
def addVectors(v1, v2):
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)
print(addVectors(a,b))
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]
这是另一种方法做it.It是为我工作的罚款。
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("")
j = min(len(l1), len(l2))
l3 = [l1[i]+l2[i] for i in range(j)]
如果你认为你的列表作为numpy的数组,那么你需要轻松地总结他们:
import numpy as np
third = np.array(first) + np.array(second)
print third
[7, 9, 11, 13, 15]
您可以使用此方法,但如果两个名单是同样大小的它只会工作:
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