python - 如何在不使用数组的情况下以交替方式将两个序列合并为单个序列?

标签 python math

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/

相关文章:

performance - 获取第 i 位 - % 还是 & 更快?

math - float - 最接近 1.7 的数

python - 如何将新的 ".where()"添加到现有的 peewee 查询中?

python - 如何用值 [ODOO 12] 更新 One2many 列表

objective-c - 使用 + 或 - 运算符对两个 CGPoints 进行算术运算

r - 将不同大小的行变成列

math - 找到优化的旋转

python - Django 3.2 属性错误 : 'TextField' object has no attribute 'db_collation'

python - 如何在 CPython 2.7.2 中停用方法缓存?

python 3.3 : Unable to load an image using Pygame if game folder is not in C drive