-1

Say you have a list [1,2,3,4] And I want to get [2,3,4,1] or [3,4,1,2]. Basically I am using the list with a different starting point each time, but then continuing through the list. How would I create something to recognize that in python.

What i have now is list[n:] where n is the shifted value, say 2, making you start at three.

4

2 回答 2

3
someList[n:] + someList[:n] 

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

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

于 2013-03-15T17:56:06.000 回答
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]
>>> 
于 2013-03-15T17:54:57.487 回答