浏览列表

时间:2013-03-15 17:49:18

标签: python list

说你有一个清单[1,2,3,4] 我想得到[2,3,4,1]或[3,4,1,2]。 基本上我每次使用具有不同起点的列表,但随后继续列表。我如何在python中创建一些东西来识别它。

我现在拥有的是list [n:],其中n是移位值,比如2,让你从三开始。

2 个答案:

答案 0 :(得分:3)

someList[n:] + someList[:n] 

会解决您的目的if n <= len(someList)

此外,collections.deque是有效的方法。

答案 1 :(得分:2)

我相信这就是你想要的

>>> def startAt(index, list):
...     print list[index:] + list[:index]
... 
>>> l = [0,1,2,3,4,5]
>>> startAt(3, l)
[3, 4, 5, 0, 1, 2]
>>> 
相关问题