将两个 LISTS 的值之和添加到新 LIST 中
- 2025-03-18 08:54:00
- admin 原创
- 37
问题描述:
我有以下两个列表:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
现在我想将这两个列表中的项目添加到一个新列表中。
输出应该是
third = [7,9,11,13,15]
解决方案 1:
此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]
解决方案 2:
numpy.add
(等)中的默认行为numpy.subtract
是逐元素的:
import numpy as np
np.add(first, second)
输出
array([7,9,11,13,15])
解决方案 3:
来自文档
import operator
list(map(operator.add, first,second))
解决方案 4:
假设两个列表a
具有b
相同的长度,则不需要 zip、numpy 或其他任何东西。
Python 2.x 和 3.x:
[a[i]+b[i] for i in range(len(a))]
解决方案 5:
尝试以下代码:
first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map(sum, zip(first, second))
解决方案 6:
这可以扩展到任意数量的列表:
[sum(sublist) for sublist in itertools.izip(*myListOfLists)]
myListOfLists
就你的情况来说,[first, second]
解决方案 7:
简单且快捷的方法是:
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])
解决方案 8:
单行解决方案
list(map(lambda x,y: x+y, a,b))
解决方案 9:
first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
three = list(map(sum, first, second))
print(three)
# Output
[7, 9, 11, 13, 15]
解决方案 10:
如果您有未知数量的相同长度的列表,则可以使用以下函数。
此处 args 接受可变数量的列表参数(但仅对每个列表中相同数量的元素求和)。再次使用 来解包每个列表中的元素。
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]
或者有 3 个列表
sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])
输出:
[19, 19, 19, 19, 19]
解决方案 11:
我的回答与 Thiru 在 3 月 17 日 9:25 的回答相同。
它更简单、更快捷,以下是他的解决方案:
简单且快捷的方法是:
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])
你需要 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]
解决方案 12:
如果你有一个不同长度的列表,那么你可以尝试这样的方法(使用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]
解决方案 13:
您可以使用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]
解决方案 14:
还有另一种方法。我们利用 python 的内部 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]
解决方案 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))
解决方案 16:
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]
解决方案 17:
这是另一种方法。对我来说效果很好。
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("")
解决方案 18:
j = min(len(l1), len(l2))
l3 = [l1[i]+l2[i] for i in range(j)]
解决方案 19:
如果你将列表视为 numpy 数组,那么你需要轻松地对它们求和:
import numpy as np
third = np.array(first) + np.array(second)
print third
[7, 9, 11, 13, 15]
解决方案 20:
也许最简单的方法是:
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)
解决方案 21:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
third=[]
for i,j in zip(first,second):
t=i+j
third.append(t)
print("Third List=",third)
output -- Third List= [7, 9, 11, 13, 15]
解决方案 22:
在这个问题中,你的代码得到了两个分子量列表。
打印:
每个列表的第二低分子量(2 个数字) 两个列表的总平均分子量(单个数字) 创建一个新列表,其中包含两个列表中的所有元素,但最大值和最小值除外(两个列表中除 2 个值之外的所有值)。然后对该列表进行排序并打印。
注意:每个列表中元素的数量不是固定的。
变量名称:mws1、mws2 如果需要,您可以在代码中定义其他变量。请参阅输出示例。
输出示例 (mws1 = [71, 88, 90], mws2 = [77, 5, 24, 65]): 第一个列表的第二低值:88 第二个列表的第二低值:24 合并列表的平均值:60.0 两个列表中的排序值,不包括极值:[24, 65, 71, 77, 88]
输出的另一个示例 (num = mws1 = [26, 34, 14, 66, 78], mws2 = [98, 15, 88]): 第一个列表的第二低值:26 第二个列表的第二低值:88 合并列表的平均值:52.375 两个列表中的排序值,不包括极值:[15, 26, 34, 66, 78, 88]
输出的另一个示例(mws1 = [10, 53, 70, 1], mws2 = [22, 27]): 第一个列表的第二低值:10 第二个列表的第二低值:27 合并列表的平均值:30.5 两个列表中的排序值,不包括极值:[10, 22, 27, 53]
解决方案 23:
您可以使用此方法,但只有两个列表的大小相同时它才有效:
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
扫码咨询,免费领取项目管理大礼包!