在python3中,如果我有一个列表[a,b,c,d,e]
,其排名顺序[1,4,2,2,0]
与元素c
和d
共享相同的顺序。如何获取新列表[e,a,c,d,b]
?
答案 0 :(得分:0)
如果您正在寻找与“冒泡排序”相似的详细算法:
original = [a,b,c,d,e]
order = [1,4,2,2,0]
# Initialize list
new = [None for x in range(len(original))]
for i, item in enumerate(original):
idx = order[i]
if new[idx] is None:
new[idx] = item
else:
while True:
if new[idx] < item:
idx += 1
if new[idx] is None:
new[idx] = item
break
else:
temp = new[idx]
new[idx] = item
while True:
idx += 1
if new[idx] is None:
new[idx] = temp
break
else:
temp2 = new[idx]
new[idx] = temp
temp = temp2