python合并列表重新排序_python – 将两个已排序的列表合并为一个更大的排序列表...

我正在尝试创建一个合并函数,将在我正在进行的合并排序中使用.

我遇到了一些麻烦,我似乎无法找到错误.

我评论它试图向你们展示我的思考过程:

def merge(aList, bList):

newList = []

while (len(aList) > 0) & (len(bList) > 0): #Loop until both lists are empty

if aList[0] < bList[0]: #If the first item of aList is smaller than the first item of bList

newList.append(aList[0]) #add that item to the new list

aList.pop(0) #and remove it from the original list

else: #If it gets here, that means the first item of bList was smaller

newList.append(bList[0]) #So put the first item of bList is the new list

bList.pop(0) #and remove it from the original

return newList

list1 = [3, 4, 8, 9]

list2 = [1, 2, 5, 8]

print(merge(list1, list2))

print(list1)

print(list2)

输出:

[1, 2, 3, 4, 5, 8]

[8, 9]

[0]

我期待list1和list2为空,但由于某种原因,list1中似乎有一个未放置的8和9.有人有想法吗?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值