Python--合并两个有序列表
#!/usr/bin/python# -*- coding: utf-8 -*-list1 = [3, 7, 8, 9, 12]list2 = [5, 6, 10, 13, 25, 30]result = []while list1 and list2:if list1[0] < list2[0]:result.append(list1[0])l
·
#!/usr/bin/python
# -*- coding: utf-8 -*-
list1 = [3, 7, 8, 9, 12]
list2 = [5, 6, 10, 13, 25, 30]
result = []
while list1 and list2:
if list1[0] < list2[0]:
result.append(list1[0])
list1.remove(list1[0])
else:
result.append(list2[0])
list2.remove(list2[0])
#list1元素已全部添加到result,此时只需将剩余的元素添加到result中
for i in list2:
result.append(i)
print(result)
更多推荐
已为社区贡献1条内容
所有评论(0)