python算法实现两个顺序列表的合并

题目:有顺序列表a[1,3,5,7,9],b[2,3,7,9],如何实现合并?

第一种

A = [1, 2, 3, 4, 5, 6, 99]
B = [2, 4, 6, 8, 10]
i = 0
j = 0
aLen = len(A)
bLen = len(B)
while i < bLen:
    bEle = B[i]
    while j < aLen:
        aEle = A[j]
        if bEle < aEle:
            print bEle,
            break
        else:
            print aEle,
            j += 1
    i += 1
while i < bLen:
    print B[i],
    i += 1
while j < aLen:
    print A[j],
    j += 1

第二种

A = [1,2,3,4,5,6]
B = [2,4,6,8,10]
C=A+B
C.sort()
print(C)

你可能感兴趣的:(python,算法)