473,396 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

arrays

Hi.
Is there someway i can get something similar to multi-dimensional
arrays in python.I dont want to use Numarray.
rahul
Jul 18 '05 #1
7 1731
>>>>> "Rahul" == Rahul Garg <co********@gmail.com> writes:

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

Jul 18 '05 #2
Rahul Garg wrote:
Hi.
Is there someway i can get something similar to multi-dimensional
arrays in python.I dont want to use Numarray.
rahul


Use lists in lists.
foo = [[1,2], [3,4]]
print foo[0][1]

2
--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
Robert Kern wrote:
Rahul Garg wrote:
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.


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
Sc***********@Acm.Org

Jul 18 '05 #4
co********@gmail.com (Rahul Garg) wrote in message news:<a2*************************@posting.google.c om>...
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.
--
CARL BANKS
Jul 18 '05 #5
Scott David Daniels wrote:
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
Sc***********@Acm.Org
Jul 18 '05 #6
>>>>> "Rahul" == Rahul Garg <co********@gmail.com> writes:

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.
Jul 18 '05 #7
Isaac To wrote:
>> "Rahul" == Rahul Garg <co********@gmail.com> writes:


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) ]
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
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...
0
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...

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.