开发者

Python: Multi-dimensional array of different types

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 c开发者_如何学Goontinuous, 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?


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!


In many cases such arrays are not required as there are more elegant solutions to these problems. Explain what you want to do so someone can give some hints.

Anyway, if you really, really need such data structure, use array.array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜