I want to create a logic that adds two lists of different len's. I want to add list1=[1,2,3,4,5] and list2=[1,1,1,1,1,1,1] to make list3=[2,3,4,5,6,1,1]. But I keep getting errors ㅠ 혹시 Can you make the code easier than this?
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)
list1=[1,2,3,4,5] #len=5
list2=[1,1,1,1,1,1,1] #len=7
if len(list2) > len(list1):
for i in range(len(list1)):
list2[i] = list2[i] + list1[i]
print(list2)
else:
for i in range(len(list2)):
list1[i] = list1[i] + list2[i]
print(list1)
Just add the element of the longest list to the element of the smallest list When the for statement ends, it will be simple if you print out a long list.
And the reason why the error occurs in the code is
555 Who developed the "avformat-59.dll" that comes with FFmpeg?
563 Uncaught (inpromise) Error on Electron: An object could not be cloned
803 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
555 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.