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

2009-04-11 Thread ergconcepts
Hi,
I have a list looking like

[ 0.84971586,  0.05786009,  0.9645675,  0.84971586,  0.05786009,
0.9645675, 0.84971586,  0.05786009,  0.9645675,  0.84971586,
0.05786009,  0.9645675]

and I would like to break this list into subsets of fixed length (say,
three elements), i.e. to convert the list into a form such as the one
generated by the following example code which I have found:

>>>import numpy
>>>s = numpy.random.random((3,3))
>>>s
array([[ 0.11916176,  0.96409475,  0.72602155],
   [ 0.84971586,  0.05786009,  0.96456754],
   [ 0.81617437,  0.845342  ,  0.09109779]])

How can I create such a 2d array (i.e., something like a symmetric
matrix) from my data?

Thanks in advance,

Bernard

PS: Note that the numpy import is not important here, it is just the
structure of the data that matters..
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-04-11 Thread ergconcepts
On Apr 11, 10:37 pm, Andreas Pfrengle  wrote:

> my_list = []
> for x in range(3):
>  my_list.append([])
>  for y in range(3):
>   my_list[x].append(some_value)

Thanks for your help - but I'm sorry I do not understand:


> my_list = []
I guess here you are implying to write something like
my_list = [ 0.84971586,  0.05786009,  0.9645675,  0.84971586,
0.05786009, 0.9645675, 0.84971586,  0.05786009,  0.9645675,
0.84971586, 0.05786009,  0.9645675]
?

then what would this do:
> my_list.append([])
?

and finally, what do you mean by "some_value"?
>   my_list[x].append(some_value)
of course, the value to append would always be the next one from my
list, and not one I would want to give explicitly each time..

I'm sorry if I did not make myself very clear before but I am
completely new to the python syntax and also to the technical terms..

Best regards,

Bernhard
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-04-11 Thread ergconcepts
On Apr 11, 11:18 pm, George Sakkis  wrote:

> The numpy import *is* important if you want to use numpy-specific
> features; there are many "tricks" you can do easily with numpy arrays
> that you have to write manually for, say, regular python lists. For
> example what you want to do is trivial with numpy:
>
> >>>import numpy as N
> >>> s = N.array([ 0.84971586,  0.05786009,  0.9645675,  0.84971586,  
> >>> 0.05786009,
>
> 0.9645675, 0.84971586,  0.05786009,  0.9645675,  0.84971586,
> 0.05786009,  0.9645675])>>> # convert to a 4by3 array in place
> >>> s.shape = (4,3)
> >>> s
>
> array([[ 0.84971586,  0.05786009,  0.9645675 ],
>        [ 0.84971586,  0.05786009,  0.9645675 ],
>        [ 0.84971586,  0.05786009,  0.9645675 ],
>        [ 0.84971586,  0.05786009,  0.9645675 ]])

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?

Thanks a lot again,

Bernard
--
http://mail.python.org/mailman/listinfo/python-list