absolute newbie: divide a list into sublists (nested lists?) of fixed length
Andreas Pfrengle
a.pfrengle at gmail.com
Sat Apr 11 22:37:25 CEST 2009
On 11 Apr., 22:14, ergconce... at googlemail.com wrote:
> 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..
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)
More information about the Python-list
mailing list