reshape a list?

Michael Spencer mahs at telcopartners.com
Mon Mar 6 23:25:32 EST 2006


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/trunk/test_pyarray.py
User docs at: http://svn.brownspencer.com/pyarray/trunk/pyarray_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




More information about the Python-list mailing list