views:

309

answers:

7

I read that Python does not actually support 2D arrays but rather an array of an array. I understand the array of an array thing but what does it mean by supporting 2D arrays?

In C a 2D array is simply converted to a 1D array by doing some fancy math (Seen here). Are there languages that implement actual 2D arrays?

Thanks for the help and I hope I haven't written a duplicate question; if so give me a link and close my question :)

+8  A: 

There are languages that implement 2D (or 3D, etc) arrays. Fortran is one of them. It means you can write an array index expression like array[x,y] and the language will take care of the math to find the correct element.

Also, Numpy is a numerical extension to Python that provides n-dimensional arrays.

Ned Batchelder
Numpy uses C extensions so it's back to the fancy pointer math with C arrays.
Casey
The authors of numpy use fancy pointer math. You as a user of it don't have to.
Ned Batchelder
+1  A: 

Fortran has real multi-dimensional arrays. This link talks about them in the FORTRAN 77 case.

Carl Norum
+2  A: 

C# has multi-dimensional arrays, but they are recommended against as jagged arrays (arrays of arrays) are more performant.

SLaks
Do you have a reference for that?
Greg Hewgill
http://www.codeproject.com/KB/dotnet/arrays.aspx
SLaks
http://uksbsguy.com/blogs/doverton/archive/2007/08/31/how-to-improve-array-performance-in-net-programming.aspx
SLaks
http://stackoverflow.com/questions/468832/why-are-multi-dimensional-arrays-in-net-slower-than-normal-arrays
SLaks
Interesting, thanks!
Greg Hewgill
P.S. Use of 'performant' raises the hackles. See http://weblogs.asp.net/jgalloway/archive/2007/05/10/performant-isn-t-a-word.aspx
Paddy3118
+1  A: 

I guess 'support of 2D arrays' means how the language allows you to access the data as if it was a 2D array. In c the fancy math is hidden by array constructs:

char arr[5][5];
char c = arr[2][3];

There is a way to do this by pointer arithmetics as well (which you probably refer as fancy math).

In reality memory is a big chunk of addressable bytes of data [0,1,2,3,4,5,6 ... end of memory] so there is no 'native' notion of 2D array, something somewhere has to translate programmers "row 1 col 2" to actual address in memory. Programming languages usually give you a way to hide the translation.

stefanB
+5  A: 

Somebody already mentioned numpy, and the OP commented that "it's back to the fancy pointer math with C arrays" -- that's a totally trivial implementation detail! Since the underlying memory in a (normal;-) computer can be seen as an array of bytes (or words), well then of course any data structure whatsoever is implemented in top of that array (or slices thereof) plus "fancy pointer math" -- double-ended queues, multi-dimensional arrays, binary trees, you name it, the underlying implementations will always boil down to that (just like all fancy control structures boil down to conditional and unconditional jumps at machine level, and so forth). SO WHAT?! These are implementation details, of course. numpy, just like Fortran and other languages and libraries, provides N-dimensional arrays -- no matter how it implements them "deep inside" (actually numpy's pretty up front about that, as you can easily flatten and reshape arrays -- it's pretty typical of Python to provide higher level abstractions with pretty good "hooks" on how they relate to lower-level ones;-).

E.g.,

>>> import numpy
>>> x = numpy.arange(12)
>>> x
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> x.reshape((3,4))
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>> x.reshape((4,3))
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> x.reshape((4,3))[::2, ::2]
array([[0, 2],
       [6, 8]])
>>> x.reshape((4,3))[(0,1,3), ::2]
array([[ 0,  2],
       [ 3,  5],
       [ 9, 11]])
>>>

You can reshape, index, slice and mold the data in the N-dimensional array with high flexibility and excellent performance -- even while knowing that the underlying data block is just that one-dimensional array (here x is born and stays 1-D, but even if that weren't the case you could still access the underlying 1-D array by flattening).

This is what "support for N-dimensional array" means (though in most other languages and frameworks offering such support you may get less transparency, lower functionality, or both;-).

Alex Martelli
+1 python is cool as usual ...
stefanB
+1  A: 

Actual arrays in Python - that is, the array object created by the array module - are strictly one-dimensional. They're also something you probably won't use unless your code has to interoperate with C data structures, you're working with huge quantities of data that must be kept in memory, or you're writing something like numpy.

Robert Rossney
A: 

Rosetta Code has Python examples for creating a two dimensional array.

Paddy3118