Python用切片实现循环移位

Python用切片实现循环移位 定义函数shift,用切片方式实现序列的循环移位 k>0,左移;k<0,右移python >>> 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)
相关文章
相关标签/搜索