for eg if sequence 1 : <1,3,5> and sequence 2 : <2,6,8,11,12>
than the result should be <1,2,3,6,5,8,11,12>
我尝试使用循环来解决它,但我得到的是重复的结构
最佳答案
i=0
j=0
N=len(L1)
M=len(L2)
L=[]
turn=True
while(i<N and j<M):
if turn:
L.append(L1[i])
i+=1
else:
L.append(L2[j])
j+=1
turn=!turn
while(i<N):
L.append(L1[i])
i+=1
while(j<M):
L.append(L2[j])
j+=1
print(L)
这可以为你做点什么,我正在做的是,我保持一个 bool 转向交替轮流以从两个数组中进行选择如果你对循环逻辑有任何疑问,我可以详细说明一下。 simpleturn= !turn => ALternates 每次迭代都会打开
关于python - 如何在不使用数组的情况下以交替方式将两个序列合并为单个序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66058562/