[SciPy-user] problem referencing single element arrays
Wed Apr 30 21:41:40 CDT 2008
2008/4/30 Alan Jackson <email@example.com>:
> Having a problem with numpy that has me stumped.
> I have a large set of arrays. I want to set up cross reference lists of
> elements within those arrays, with the lists just being pointers to the objects
> (floating point numbers) stored in the arrays.
> The problem is that some arrays have only a single element, so when I try to
> store a for a single element array, I get an error, "0-d arrays can't be
> indexed". How do I get a pointer to the object stored and not the array for
> single element arrays?
> Or maybe I don't want to be doing this?
Well, I'm not totally sure I know what you mean, but I think what you
want can be done in a reasonable way. Numpy has two subtly different
kinds of object, scalars and rank zero arrays; I think rank zero
arrays can be made to do what you want, more or less, but they're kind
of a neglected corner case. But a rank-1 array that happens to have
only one element should behave in a perfectly reasonable fashion, and
indexing it with  should work fine. The only trick is constructing
one the right way: if A is a rank-1 array and we want to make a view
that lets us get at element j, we can do
e = A[j:j+1]
and then, for example,
e = newvalue
More information about the SciPy-user