|
From: Travis O. <oli...@ie...> - 2006-01-24 15:57:36
|
Ed Schofield wrote: >Hi all, > >I've been attempting to use an object array to group together a >collection of different-length arrays or lists, and I'm getting >unexpected results. Here's an example: > > > >>>>import numpy >>>>a = numpy.array([[1,2],[1,2,3]],object) >>>> >>>> > >I expected here that 'a' would be an array of dimension (2,), holding >two Python lists of integers. Was I wrong to expect this? It seems >instead that 'a' is a 0-dimensional array holding one object: > > > Yes, you were wrong to expect this ;-) The array constructor is not really useful for constructing arbitrary object arrays. It is not designed to analyze what you've input and pick the "optimal" assignment. It never has been. So, it's not surprising that it doesn't do what you expect. > >[[1, 2], [1, 2, 3], [1, 2], [1, 2, 3]] > >although these last two commands return Python lists, without the 0-d >array wrapper, which isn't consistent with other 0-d arrays. > > We just changed that remember. When 0-d object arrays result from calculations, the actual objects are returned. >On the whole, 0-d object arrays seem quite strange beasts. Could >someone please enlighten me on why they deserve to exist? ;) They seem >inconsistent with the new simplified interface to object array >elements. Could we get rid of them entirely?! > > No, we can't get rid of them because 0-d arrays exist and OBJECT is a data-type an array can have. Somebody is going to be able to construct one. My advise is to create empty object arrays and fill them appropriately unless something better is written for OBJECT array construction. -Travis |