absolute newbie: divide a list into sublists (nested lists?) of fixed length

Andre Engels andreengels at gmail.com
Sun Apr 12 00:15:45 CEST 2009


On Sun, Apr 12, 2009 at 12:05 AM,  <ergconcepts at googlemail.com> wrote:

> Thanks very much - works fine! Now for a follow-up question:)
>
> Actually my original list called "mylist" contains 81217 elements - I
> shape those into
>
>>>> len(mylist)
> 81217
>>>> s = N.array(mylist)
>>>> s.shape = (241,337)
>
> which works because the "total size of new array must be
> unchanged" (241x337=81217)
>
> Now this "matrix" actually is a 2D colorcoded map - I want to plot
> this using the imshow splot routine in mayavi2..
>
> However, there might be a problem: I believe that sometimes my
> original array will not exactly contain 81217 elements, but maybe only
> 81216 or so. Nevertheless, I know that time x times y - structure will
> always be the same (that is, 241 columns). What is of interest to me
> is the first 241 x 241 part of the matrix anyway. Is there an easy way
> to reshape my original array into a symmetric 241x241 matrix?

Provided that the array always has at least 58081 elements:

[mylist[i:i+241] for i in range(0,58081,241)]




-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list