Python用切片实现循环移位 定义函数shift,用切片方式实现序列的循环移位 k>0,左移;k<0,右移 >>> def shift(ls,k): return(ls[k:]+ls[:k]) #例1,tmp左移2位 >>> ls [1, 2, 3, 4, 5, 6, 7, 8] >>> tmp = ls[:] >>> tmp2 = ls[:] >>> shift(tmp,2) [3, 4, 5, 6, 7, 8, 1, 2] #例2,tmp2右移2位 >>> shift(tmp2,-2) [7, 8, 1, 2, 3, 4, 5, 6]