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

E

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:
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..
 
A

Andreas Pfrengle

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:

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..

Not sure if that's the best solution, but you could try:

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

ergconcepts

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
 
G

George Sakkis

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:

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..

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])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 ]])


HTH,
George
 
M

Mensanator

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:
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,

PS: Note that the numpy import is not important here, it is just the
structure of the data that matters..

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

What does numpy do if the original list has 13 elements?
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 ]])

HTH,
George
 
E

ergconcepts

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
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

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
 
A

Andre Engels

Thanks very much - works fine! Now for a follow-up question:)

Actually my original list called "mylist" contains 81217 elements - I
shape those into


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)]
 
G

George Sakkis

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



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?

Yes, use numpy.resize:

You should probably be able to find the answer to most of your
upcoming numpy questions at http://numpy.scipy.org/numpydoc/numpy.html
(especially the array functions,
http://numpy.scipy.org/numpydoc/numpy-9.html)

George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top