如何根据已知订单重新排序列表?

时间:2017-12-18 23:43:13

标签: python python-3.x sorting ranking rank

在python3中,如果我有一个列表[a,b,c,d,e],其排名顺序[1,4,2,2,0]与元素cd共享相同的顺序。如何获取新列表[e,a,c,d,b]

1 个答案:

答案 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