python 数组如何排序

a1 = [2, 1, 3]b1 = [1, 1, -2]
正确答案:[3,2,1]
a1是数据,b1是下标,请问如何根据b1提供的下标对a1进行排序,其中b2里面的-2代表向左边移动2位,所以最后的结果是[3,2,1],请问大佬应该如何实现!!
回答:
Python 版
python">ret = [0] * len(a1)for (x, y), i in zip(enumerate(a1), b1):
    ret[x + i] = y
print(ret)
回答:
typescript版本
const a1 = [2, 1, 3];const b1 = [1, 1, -2];
const sort = (arr: number[], indexes: number[]): number[] => {
  const temp: number[] = Array.from({ length: arr.length });
  for (let i = 0; i < arr.length; ++i) {
    temp[i + indexes[i]] = arr[i];
  }
  return temp;
};
console.log(sort(a1, b1));
回答:
def offset_execute(arr,off):    do_offset(arr,off,0,arr[0],0)
def do_offset(arr,offset,start,item,count):
    off = offset[start]
    position = start + off
    nextItem = arr[position]
    arr[position] = item
    count = count + 1
    if(count != len(arr)):
        do_offset(arr,offset,position,nextItem,count)
arr = [2,1,3]
offset = [1,1,-2]
offset_execute(arr,offset)
print(arr)
这不应该叫排序,应该叫移动,这比排序要简单很多。
以上是 python 数组如何排序 的全部内容, 来源链接: utcz.com/p/937952.html




