From: olipt at mayo.edu (Travis Oliphant)
Date: Sat, 10 Apr 1999 10:47:43 -0500
Subject: Idiom for Getting Slices of a List
In-Reply-To: <Pine.SUN.3.95-heb-2.07.990410102503.23259A-100000@sunset.ma.huji.ac.il>
References: <Pine.SUN.3.95-heb-2.07.990410102503.23259A-100000@sunset.ma.huji.ac.il>
Message-ID: <Pine.LNX.4.04.9904101044250.7006-100000@us2.mayo.edu>
X-UID: 1440                                                  

> I want a clean, and relatively efficient method to do the following:
> I have an array of length, n*m and I want to make it an array of 
> length m, where each member is an array of length n.
> 
> Example: n=2, m=3
> [0, 1, 2, 3, 4, 5] ==> [[0, 1], [2, 3], [4, 5]]

If you have the Numeric extension:

from Numeric import reshape

>>> d = [0,1,2,3,4,5]
>>> n,m = 2,3
>>> f = reshape(d,(m,n)).tolist()
>>> print f
[[0, 1], [2, 3], [4, 5]]

Best,

Travis Oliphant






