我有名单
a=[1,3,6,9,10]
b=[2,4,5,7,8]
c=[]
如何在从a和b中删除元素时将列表添加到列表c?
答案 0 :(得分:2)
c = []
aa, bb = a.pop(0), b.pop(0)
while True:
if aa < bb:
c.append(aa)
try:
aa = a.pop(0)
except IndexError:
c += b
b = []
else:
c.append(bb)
try:
bb = b.pop(0)
except IndexError:
c += a
a = []
或者
c = sorted(a + b)
a = b = []