我正在嘗試做一個排序算法,我需要做這樣的事情:
arr = numpy.array([2,4,5,6])
#some function here or something
array([5,2,4,6])
#element "5" moved from position 3 to 1, and all of the others moved up one position
我的意思是我想改變一個元素的位置(索引)並將所有其他元素向上移動一個位置。這可能嗎?
您可以將 numpy.roll 與子集分配一起使用:
arr = numpy.array([2,4,5,6])
arr[:3] = numpy.roll(arr[:3],1)
print(arr)
[5 2 4 6]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句