Python) 我想添加两个 len 顺序不同的列表
Python) I wanna add two lists which are different order of len
我想添加 list1=[1,2,3,4,5]
和 list2=[1,1,1,1,1,1,1]
我想要的是这样的
list3=[2,3,4,5,6,1,1]
这是我的错误代码
lis1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
if len(list1)>len(list2):
for i in range(len(list1)):
list2.append(0) if list2[i]=[]
list3[i]=list1[i]+list2[i]
else:
for i in range(len(list2)):
list1.append(o) if list1[i]=[]
list3[i]=list1[i]+list2[i]
print(list3)
您可以使用 izip_longest
来自 itertools
例如:
from itertools import izip_longest
list1=[1,2,3,4,5]
list2=[1,1,1,1,1,1,1]
print([sum(i) for i in izip_longest(list1, list2, fillvalue=0)])
输出:
[2, 3, 4, 5, 6, 1, 1]
基本上你想把两个列表加在一起,如果列表不够长用0填充。所以我直接从你的代码修改而没有任何库:
list1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
list3 = [] # make a list3
if len(list1)>len(list2):
for i in range(len(list1)):
# if list2[i]=[] this line is wrong, you can't compare non-exist element to a empty array
if i >= len(list2):
list2.append(0)
list3.append(list1[i]+list2[i])
else:
for i in range(len(list2)):
if i >= len(list1):
list1.append(0)
list3.append(list1[i]+list2[i])
print(list3)
我会尽量使这段代码尽可能简单。首先,您从不 想要复制粘贴您的代码。您的 if/else 声明应该首先被评估。
lis1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
longer, shorter = [], []
if len(list1) > len(list2):
longer, shorter = list1, list2
else:
longer, shorter = list2, list1
现在我们已经通过简单地命名我们的列表 longer
和 shorter
来确定哪个列表更长,哪个更短。我们的下一个任务是编程算法。我们想要做的是迭代 longer
列表并添加我们找到的每个整数:
for i in range(len(longer)):
longer[i] += shorter[i]
print(longer)
我们尝试 运行 程序并 繁荣 ,它失败并显示 out of range exception
。所以我们确定问题,并修复我们的代码:
for i in range(len(longer)):
if (i > len(shorter)): ## We make sure to not call shorter[i] if there is no such element
longer[i] += shorter[i]
print(longer)
有问题吗?
我想添加 list1=[1,2,3,4,5]
和 list2=[1,1,1,1,1,1,1]
我想要的是这样的
list3=[2,3,4,5,6,1,1]
这是我的错误代码
lis1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
if len(list1)>len(list2):
for i in range(len(list1)):
list2.append(0) if list2[i]=[]
list3[i]=list1[i]+list2[i]
else:
for i in range(len(list2)):
list1.append(o) if list1[i]=[]
list3[i]=list1[i]+list2[i]
print(list3)
您可以使用 izip_longest
来自 itertools
例如:
from itertools import izip_longest
list1=[1,2,3,4,5]
list2=[1,1,1,1,1,1,1]
print([sum(i) for i in izip_longest(list1, list2, fillvalue=0)])
输出:
[2, 3, 4, 5, 6, 1, 1]
基本上你想把两个列表加在一起,如果列表不够长用0填充。所以我直接从你的代码修改而没有任何库:
list1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
list3 = [] # make a list3
if len(list1)>len(list2):
for i in range(len(list1)):
# if list2[i]=[] this line is wrong, you can't compare non-exist element to a empty array
if i >= len(list2):
list2.append(0)
list3.append(list1[i]+list2[i])
else:
for i in range(len(list2)):
if i >= len(list1):
list1.append(0)
list3.append(list1[i]+list2[i])
print(list3)
我会尽量使这段代码尽可能简单。首先,您从不 想要复制粘贴您的代码。您的 if/else 声明应该首先被评估。
lis1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
longer, shorter = [], []
if len(list1) > len(list2):
longer, shorter = list1, list2
else:
longer, shorter = list2, list1
现在我们已经通过简单地命名我们的列表 longer
和 shorter
来确定哪个列表更长,哪个更短。我们的下一个任务是编程算法。我们想要做的是迭代 longer
列表并添加我们找到的每个整数:
for i in range(len(longer)):
longer[i] += shorter[i]
print(longer)
我们尝试 运行 程序并 繁荣 ,它失败并显示 out of range exception
。所以我们确定问题,并修复我们的代码:
for i in range(len(longer)):
if (i > len(shorter)): ## We make sure to not call shorter[i] if there is no such element
longer[i] += shorter[i]
print(longer)
有问题吗?