473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 47392
"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 multidimensiona l arrays instead of faking it
with lists.

http://numeric.scipy.org

In [15]: import numpy

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

In [17]: a = numpy.column_st ack((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*********@gma il.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 multidimensiona l arrays instead of faking it
with lists.


Or, you can fake real multidimensiona l 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.ListVie w and pyarray.ArrayVi ew 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 multidimensiona l arrays instead of faking it
with lists.


Or, you can fake real multidimensiona l 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*********@gma il.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
4306
by: Hilde Roth | last post by:
This may have been asked before but I can't find it. If I have a rectangular list of lists, say, l = ,,], is there a handy syntax for retrieving the ith item of every sublist? I know about for i in l] but I was hoping for something more like l. Hilde
13
6770
by: Rajarshi Guha | last post by:
Hi, is there an efficient (pythonic) way in which I could split a list into say 5 groups? By split I mean the the first x members would be one group, the next x members another group and so on 5 times. (Obviously x = lengthof list/5) I have done this by a simple for loop and using indexes into the list. But it does'nt seemm very elegant Thanks,
3
1809
by: Stephen Boulet | last post by:
I have a long list of floats (1613808 elements long). It takes quite a while to load this into a 7 dimensional Numeric array. Is there any obvious way to speed this up? def insertData(self,data): # Start off with an array of the desired size # "self.MEASURED", etc., are integers arrayData = zeros(, Float64)
2
1428
by: flyaflya | last post by:
I want make a 2-D array from a list,all elements is references of list's,like this: a = b = , ] when change any elements of a, the elements of b will change too, so I can use some function for list to change b. c/c++ can work in this way,I think let b = a, b = a,but it's not reference,it's copy.
4
12339
by: KraftDiner | last post by:
I have a 2D array. Say it is 10x10 and I want a 1D Array of 100 elements... What is the syntax? oneD = reshape(twoD, (100,1)) or oneD = reshape(twoD, (1,100)) One I guess is the transpose of the other but both seem to be arrays of arrays...
2
3826
by: TG | last post by:
Hi there. Reading the page on python performance ( http://scipy.org/PerformancePython ) made me realize that I can achieve tremendous code acceleration with numpy just by using "u" kind of syntax the clever way. Here is a little problem (Oja's rule of synaptic plasticity) * W is a matrix containing the weights of connections between elements i
0
2802
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY...
0
1692
by: David d'Angers | last post by:
hi all i need to do matrix manipulations in c things like "reshape" of matlab etc and i'd like some advice for choosing a library i'm doing development in computer vision thanks
3
2045
by: pi.arctan | last post by:
Hi guys, One of my many project involves working with YUV-files, where I need to reduce the vertical resolution with a factor of two, i.e. remove every other scan line. Today I'm using two for-loops in the fashion shown below y = for i in range(0, width*height, width*2):
0
8010
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7942
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8300
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6761
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3969
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1550
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.