Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Multi-dimensional array of different types

Tags:

python

arrays

Is it possible to create a multi-dimensional array of different types in Python? The way I usually solve it is [([None] * n) for i in xrange(m)], but I don't want to use list. I want something which is really a continuous array of pointers in memory, not a list. (Each list itself is continuous, but when you make a list of lists, the different lists may be spread in different places in RAM.)

Also, writing [([None] * n) for i in xrange(m)] is quite a convoluted way of initializing an empty array, in contrast to something like empty_array(m, n). Is there a better alternative?

like image 637
Ram Rachum Avatar asked Feb 05 '11 21:02

Ram Rachum


1 Answers

If you're using numpy, by chance, numpy object arrays are "continuous arrays of pointers in memory".

However, they defeat the usual purpose of numpy arrays, and often wind up being a bad answer to the problem at hand...

(contiguous memory of the same type --> fast calculations on the entire array... Object arrays don't allow this, as they're just arrays of pointers to python objects.).

Nonetheless, np.empty((m,n), dtype=np.object) does what you want.

E.g.

x = np.empty((3,4), dtype=np.object)
print x
x[2,3] = range(5)
x[1,2] = 2
x[1,3] = (item*2 for item in xrange(10))
print x

Which yields:

Initial array:
[[None None None None]
 [None None None None]
 [None None None None]]

Modified array:
[[None None None None]
 [None None 2 <generator object <genexpr> at 0x8700d9c>]
 [None None None [0, 1, 2, 3, 4]]]

Just be aware that it's going to be much slower and much less memory efficient than "normal" numpy arrays! (i.e. Even a None object takes up quite a bit of memory compared to a (numpy, not python) float, and you've got the additional overhead of the pointer stored in the array. A big object array will use tons of memory!)

However, if what you need is effectively a multi-dimensional list, and you're not going to be appending to it or changing its size frequently, they can be damned convenient. They're essentially meant to be equivalent to matlab's cell arrays, and while there's less of a need for that sort of a data structure in python (python has lists), sometimes it's handy!

like image 95
Joe Kington Avatar answered Nov 03 '22 06:11

Joe Kington