reshape a list? | Bytes (2024)

KraftDiner

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

Subscribe Reply

5 reshape a list? | Bytes (1) 47383 reshape a list? | Bytes (2)

Fredrik Lundh

"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

Robert Kern

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

Michael Spencer

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]]])
aarray([[[ 1, 2, 3],
[ 4, 5, 6]],

[[ 7, 8, 9],
[10, 11, 12]]]) a.flatarray([ 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)
aarray([[ 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

Robert Kern

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

KraftDiner

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 4288

Indexing list of lists

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

Python

13 6761

splitting a list into n groups

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

Python

3 1807

Long list to numeric multi-D array

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)

Python

2 1426

Can get reference of a part of a list?

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.

Python

4 12338

reshape an array?

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

Python

2 3825

numpy performance and list comprehension

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

Python

2796

native python matrix class (2D list), without inverse

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

Python

1690

how to implement matlab's "reshape" etc

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

C / C++

3 2041

extract multiple ranges from a list

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

Python

7518

What is ONU?

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

General

7711

Problem With Comparison Operator <=> in G++

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

C / C++

7954

Maximizing Business Potential: The Nexus of Website Design and Digital Marketing

by: jinu1996 |last post by:

In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...

Online Marketing

1 7467

The easy way to turn off automatic updates for Windows 10/11

by: Hystou |last post by:

Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...

Windows Server

7805

Discussion: How does Zigbee compare with other wireless protocols in smart home applications?

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

General

5085

Couldn’t get equations in html when convert word .docx file to html file in C#.

by: conductexam |last post by:

I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...

C# / C Sharp

3497

Trying to create a lan-to-lan vpn between two differents networks

by: TSSRALBI |last post by:

Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

Networking - Hardware / Configuration

3478

Windows Forms - .Net 8.0

by: adsilva |last post by:

A Windows Forms form does not have the event Unload, like VB6. What one acts like?

Visual Basic .NET

1 1932

transfer the data from one system to another through ip address

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

C# / C Sharp

reshape a list? | Bytes (2024)
Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6043

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.