2013-03-15 84 views
-1

假設您有一個列表[1,2,3,4] 而且我想獲得[2,3,4,1]或[3,4,1,2]。 基本上我每次都用不同的起點使用列表,然後繼續完成列表。我將如何創建一些東西來識別python。查看列表

我現在擁有的是list [n:],其中n是移位的值,比如2,讓你從3開始。

+0

你想知道什麼? – mydogisbox 2013-03-15 17:51:34

回答

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] 
>>> 
3
someList[n:] + someList[:n] 

將解決你的目的if n <= len(someList)

而且,collections.deque是有效的方法。

+0

假設'n askewchan 2013-03-15 17:57:36

+0

@askewchan:感謝您指出它出。編輯代碼。 – GodMan 2013-03-15 17:58:54

+1

謝謝,+1。另外,你可以首先使用'n%= len(someList)'。 – askewchan 2013-03-15 18:01:51