arrays

R

Rahul Garg

Hi.
Is there someway i can get something similar to multi-dimensional
arrays in python.I dont want to use Numarray.
rahul
 
J

John Hunter

Rahul> Hi. Is there someway i can get something similar to
Rahul> multi-dimensional arrays in python.I dont want to use
Rahul> Numarray

Yes, you can use lists of lists for example.

x = [ [None for i in range(10)] for j in range (12)]
x[1][2] = 1

But it would help if you stated what your requirements are for the
multi-dim array, what kind of objects you want to store in them, what
kind of operations you need to be able to do, what your performance
requirements are, and why you don't want to use numarray or Numeric
for that matter.


JDH
 
R

Rahul Garg

Hi.
Thanks for the help. Its really simple and i shud have thought it
myself.
Some clarifications :
1. I will mostly be storing floating point numbers in 2 dimensional
arrays which i will pass to a custom module written in C. The
application is for scientific computing purposes.I just need python +
wxPython for the GUI.

2.I am not using Numarray because i dont expect to do many operations
on the matrices in Python itself. Most of that stuff will be handled
in my C module.
The decision of not doing the matrix operations in Python is because
most of the C code is already ready. Though it is written as
standalone command line app currently , converting it into an
extension module shudnt be much of a problem.

Rahul


John Hunter said:
Rahul> Hi. Is there someway i can get something similar to
Rahul> multi-dimensional arrays in python.I dont want to use
Rahul> Numarray

Yes, you can use lists of lists for example.

x = [ [None for i in range(10)] for j in range (12)]
x[1][2] = 1

But it would help if you stated what your requirements are for the
multi-dim array, what kind of objects you want to store in them, what
kind of operations you need to be able to do, what your performance
requirements are, and why you don't want to use numarray or Numeric
for that matter.


JDH
 
R

Robert Kern

Rahul said:
Hi.
Thanks for the help. Its really simple and i shud have thought it
myself.
Some clarifications :
1. I will mostly be storing floating point numbers in 2 dimensional
arrays which i will pass to a custom module written in C. The
application is for scientific computing purposes.I just need python +
wxPython for the GUI.

2.I am not using Numarray because i dont expect to do many operations
on the matrices in Python itself. Most of that stuff will be handled
in my C module.
The decision of not doing the matrix operations in Python is because
most of the C code is already ready. Though it is written as
standalone command line app currently , converting it into an
extension module shudnt be much of a problem.

You still might want to use numarray or Numeric in this case. The memory
representation of a numarray/Numeric array is the same as in C. You
won't have to duplicate memory and waste time converting between a
memory block of doubles and lists of lists of Python floats. Speaking
from experience, I would say that numarray/Numeric greatly eases the
burden of communicating arrays between Python and C even if one does not
need to do anything complicated with the arrays in Python.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
S

Scott David Daniels

Robert said:
You still might want to use numarray or Numeric in this case. The memory
representation of a numarray/Numeric array is the same as in C. You
won't have to duplicate memory and waste time converting between a
memory block of doubles and lists of lists of Python floats.

If you want to refer to a block of memory that you share with your C
code (especially if the C code controls the memory, you might also want
to check out my "Blocks and Views" code at:

http://members.dsl-only.net/~daniels/block.html

-Scott David Daniels
(e-mail address removed)
 
C

Carl Banks

Hi.
Is there someway i can get something similar to multi-dimensional
arrays in python.I dont want to use Numarray.
rahul

It seems (from other posts) as if you're looking for a way to get
simple multidimensional array behavior in Python, while making
convenient to access it in C, without using numarray or Numeric.

Probably the simplest way to accomplish this without numarray is to
use a class to wrap a list. Override __getitem__ and __setitem__ to
get the effect of multidimensional access. Because the data is stored
in a single list, rather than a list of lists, it is convenient to
access it from C. A minimal example follows.

class MinimalMultiArray:
def __init__(self,rows,cols):
self.rows = rows
self.cols = cols
self.data = [0.0] * self.rows * self.cols
def __getitem__(self,(row,column)):
return self.data[row*self.cols + column]
def __setitem__(self,(row,column),value):
self.data[row*self.cols + column] = value

Notice that Python can unwrap tuples right into function arguments, so
if you reference array[1,2], in __getitem__, row will be set to one
and column to two.

Any improvements left as an exercise. Here's a suggestion: check
whether row and column arguments are slice objects, and return a list
or a smaller multidimensional array if so.
 
S

Scott David Daniels

Scott said:
If you want to refer to a block of memory that you share with your C
code (especially if the C code controls the memory, you might also want
to check out my "Blocks and Views" code at:

http://members.dsl-only.net/~daniels/block.html

To follow up on my own post, once you have a View v of, say, 100
floating point data entries:
>>> multidim = [v[x : x+1] for x in range(0, 100, 10)]
>>> revdim = [v[x : 100 : 10] for x in range(10)]
>>> v[27] 43.6
>>> multidim[2][7] 43.6
>>> revdim[7][2] 43.6
>>> multidim[2][7] = 19.1
>>> multidim[2][7] 19.1
>>> revdim[7][2] 19.1
>>> v[27]
19.1

That is, you maintain the mapping to the original memory when taking
strides. All subscript operations change how the memory is referenced,
not take copies of the memory.

--Scott David Daniels
(e-mail address removed)
 
I

Isaac To

Rahul> Hi. Is there someway i can get something similar to
Rahul> multi-dimensional arrays in python.I dont want to use
Rahul> Numarray.

Is an array of array sufficient? Thinks like [[0]*5]*3.

Regards,
Isaac.
 
D

Duncan Booth

Isaac said:
Rahul> Hi. Is there someway i can get something similar to
Rahul> multi-dimensional arrays in python.I dont want to use
Rahul> Numarray.

Is an array of array sufficient? Thinks like [[0]*5]*3.
You were right to call that an array of array (singular): what you
suggested creates a list containing 3 references to the same list. I
suspect the OP is much more likely to want an array of arrays (plural).

The cleanest way these days is to use a list comprehension for all but the
innermost list (provided the initial value is immutable):

[[0]*5 for i in range(3)]

or, if you want something mutable or more complex for an initial value you
can just use list comprehensions throughout e.g.:

[ [(i*100+j) for i in range(5)] for j in range(3) ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,694
Latest member
digiadvancemarketing

Latest Threads

Top