Python—合并两个有序列表

def hb(list1,list2):
    result = []
    while list1 and list2:
        if list1[0] < list2[0]:
            result.append(list1[0])
            del list1[0]
        else:
            result.append(list2[0])
            del list2[0]
    if list1:
        result.extend(list1)
    if list2:
        result.extend(list2)
    print(result)
    return result

list1 = [3,4,7,9,11]
list2 = [1,2,5,8,13,20]
hb(list1, list2)

>>> [1, 2, 3, 4, 5, 7, 8, 9, 11, 13, 20]

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/10469831.html
今日推荐