Python 如何将列表中的项目向下移动两个单位

Python 如何将列表中的项目向下移动两个单位,python,slice,Python,Slice,如何将列表中的项目向下移动两个单位 例如,将数字“1”向下移动两个单位 >>> move([1,2,3,4]) [2,3,1,4] 正如斯威尼罗德指出的,不需要切片: def move (l, from_, to = 2): return l.insert (to, l.pop (from_) ) 为什么要使用列表切片?为什么不仅仅是list.\ux.insert(2,list.\ux.pop(0))?numpy.array([1,2,3,4])[[1,2,0,3]

如何将列表中的项目向下移动两个单位

例如,将数字“1”向下移动两个单位

>>> move([1,2,3,4])
[2,3,1,4]

正如斯威尼罗德指出的,不需要切片:

def move (l, from_, to = 2):
    return l.insert (to, l.pop (from_) )

为什么要使用列表切片?为什么不仅仅是
list.\ux.insert(2,list.\ux.pop(0))
numpy.array([1,2,3,4])[[1,2,0,3]]
是一种方法……谢谢大家,我会按照斯威尼罗德的建议去做。