469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

reshape a list?

I have a list that starts out as a two dimensional list
I convert it to a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?

Mar 6 '06 #1
5 45894
"KraftDiner" wrote:
I have a list that starts out as a two dimensional list
I convert it to a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?


(you could of course keep a pointer to the original 2D list...)

anyway, to split a 1D list up in pieces, use slice notation. e.g.

step = 10

a = []
for i in range(0, len(b), step):
a.append(b[i:i+step])

or, in one line:

a = [b[i:i+step] for i in range(0, len(b), step)]

for more on list slicing, see the Python tutorial.

</F>

Mar 6 '06 #2
KraftDiner wrote:
I have a list that starts out as a two dimensional list
I convert it to a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?


Alternatively, you could use real multidimensional arrays instead of faking it
with lists.

http://numeric.scipy.org

In [15]: import numpy

In [16]: tmp = numpy.arange(256)**2

In [17]: a = numpy.column_stack((a,)*256)

In [18]: a[:10,:10]
Out[18]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[16, 16, 16, 16, 16, 16, 16, 16, 16, 16],
[25, 25, 25, 25, 25, 25, 25, 25, 25, 25],
[36, 36, 36, 36, 36, 36, 36, 36, 36, 36],
[49, 49, 49, 49, 49, 49, 49, 49, 49, 49],
[64, 64, 64, 64, 64, 64, 64, 64, 64, 64],
[81, 81, 81, 81, 81, 81, 81, 81, 81, 81]])

In [19]: a.shape
Out[19]: (256, 256)

In [20]: a.ravel()[:100]
Out[20]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0])

In [21]: a.ravel().shape
Out[21]: (65536,)

In [22]: b = numpy.reshape(a.ravel(), (256,256))

In [23]: b.shape
Out[23]: (256, 256)

In [24]: (a == b).all()
Out[24]: True

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Mar 6 '06 #3
Robert Kern wrote:
KraftDiner wrote:
I have a list that starts out as a two dimensional list
I convert it to a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?


Alternatively, you could use real multidimensional arrays instead of faking it
with lists.


Or, you can fake real multidimensional arrays with lists ;-)

pyarray is a pure-Python single-module implementation of a multi-dimensional
array type.

Download from http://svn.brownspencer.com/pyarray/trunk/pyarray.py

Simple example:
import pyarray
a = pyarray.ndlist([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
a array([[[ 1, 2, 3],
[ 4, 5, 6]],

[[ 7, 8, 9],
[10, 11, 12]]]) a.flat array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

Tests at: http://svn.brownspencer.com/pyarray/...est_pyarray.py
User docs at: http://svn.brownspencer.com/pyarray/...rray_usage.txt

pyarray.ListView and pyarray.ArrayView offer a substantial subset of
numpy.ArrayType functionality, by wrapping standard python 'list' and
'array.array' respectively.

Key features include:
* Views: all subscripting operations apart from individual cell access
access return views to existing 'live' data
* Extended Indexing: slicing, arbitrary 'takes', index arrays etc...
* Unlimited re-shaping: while still addressing one data-source
* Elementwise binary operations: all basic arithmetic and comparison
operations
* Data broadcasting: allows assignment and binary operations between
views of different shapes
* Friendly __repr__: work safely with big arrays at the interactive prompt
Another example:
tmp = pyarray.arange(256)**2
a = pyarray.ndlist([tmp]*256)
a array([[ 0, 1, 4, ..., 64009, 64516, 65025],
[ 0, 1, 4, ..., 64009, 64516, 65025],
[ 0, 1, 4, ..., 64009, 64516, 65025],
...,
[ 0, 1, 4, ..., 64009, 64516, 65025],
[ 0, 1, 4, ..., 64009, 64516, 65025],
[ 0, 1, 4, ..., 64009, 64516, 65025]])
a.transpose() array([[ 0, 0, 0, ..., 0, 0, 0],
[ 1, 1, 1, ..., 1, 1, 1],
[ 4, 4, 4, ..., 4, 4, 4],
...,
[64009, 64009, 64009, ..., 64009, 64009, 64009],
[64516, 64516, 64516, ..., 64516, 64516, 64516],
[65025, 65025, 65025, ..., 65025, 65025, 65025]])
a[:10,:10] array([[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81],
[ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81]])


Michael

Mar 7 '06 #4
Michael Spencer wrote:
Robert Kern wrote:
KraftDiner wrote:
I have a list that starts out as a two dimensional list
I convert it to a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?


Alternatively, you could use real multidimensional arrays instead of faking it
with lists.


Or, you can fake real multidimensional arrays with lists ;-)

pyarray is a pure-Python single-module implementation of a multi-dimensional
array type.

Download from http://svn.brownspencer.com/pyarray/trunk/pyarray.py


That's fine, too. :-)

It's just that trying to do it *again* is a silly waste of time.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Mar 7 '06 #5
I'm wondering if your solution fits my requirements....
I need to be able to pass these objects which are python lists
to a pyObjC (on MAC OS X) framework. At the moment the
framework is expecting NSArray or NSData as input and this works
for 'generic' python lists.... I don't know about numpy arrays...

Mar 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Hilde Roth | last post: by
13 posts views Thread by Rajarshi Guha | last post: by
3 posts views Thread by Stephen Boulet | last post: by
2 posts views Thread by flyaflya | last post: by
4 posts views Thread by KraftDiner | last post: by
2 posts views Thread by TG | last post: by
reply views Thread by DarrenWeber | last post: by
reply views Thread by David d'Angers | last post: by
3 posts views Thread by pi.arctan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.