2016-12-13 52 views
0

我想重新排序Python列表:重排列表在Python

a = [1,2,3,4,5,6,7,...] 

以下形式:

[[1,2,3],[2,3,4],[3,4,5],...] 

什么是做到这一点的最快的方法?

+0

http://stackoverflow.com/questions/6614891/turning-a-list-into-nested-lists-in-python – Harsha

+1

在源代码到底会发生什么清单?你只是在结果中得到一个两元素列表和一个元素列表? –

+1

这不是所给链接的重复。 – iFlo

回答

1

你可以试试:

>>> a = [1,2,3,4,5,6,7] 
>>> new_list = [] 
>>> for index in range(len(a)-2): 
    new_list.append(a[index:index+3]) 


>>> new_list 
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7]] 
+0

或尝试zip(a,a [1:],a [2:]) – Setop