python的史蒂芬加速迭代法_带有两个迭代器的python一次递增1

我是python的新手.

我尝试在python中实现诸如归类排序之类的操作.我使用https://pythonhosted.org/llist/#sllist-objects的单链列表.要合并两个排序的列表,我需要使用迭代器遍历两个列表.伪代码如下所示:

n3 = sllist()

for n1 in list1 and n2 in list2:

if (n1 > n2):

n3.append(n1)

n1++ # c way of doing thing

else:

n3.append(n2)

n2++ # c way of doing thing

但是我不知道如何使它在python中工作.任何指针或提示都会有所帮助.

编辑:

在所有讨论和建议之后,我想到了类似这样的代码.谁能告诉我,如何摆脱最后两个while循环.我打算使用“扩展”,但无法使用它.

final_list = sllist()

node1 = list1.first

node2 = list2.first

while node1 and node2:

if node1.value < node2.value:

final_list.append(node1)

node1 = node1.next

else:

final_list.append(node2)

node2 = node2.next

while node1:

final_list.append(node1)

node1 = node1.next

while node2:

final_list.append(node2)

node2 = node2.next

return final_list

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值