[Numpy-discussion] Structured array with no fields - possible?

Christopher Barker Chris.Barker at noaa.gov
Tue May 5 11:20:59 EDT 2009


Matthew Brett wrote:
> I'm afraid what I need is some way of representing the fact that I
> have read, from matlab, a structure with no fields (and therefore no
> data) that can be - say - shape (10,2) - or any other.

how about:
 >>> a = np.empty(size, dtype=np.object)
 >>>
 >>> a
array([[None, None, None, None],
        [None, None, None, None],
        [None, None, None, None]], dtype=object)

I also thinking of putting an empty as the items, but I couldn't figure 
out how to do that:

 >>> a[:] = ()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape
 >>> a[0] = ()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: shape mismatch: objects cannot be broadcast to a single shape

Some folks think the way to spell a struct in python is a clas with only 
attributes, so:

 >>> class empty:
...   def __repr__(self):
...     return "empty class"
...

 >>> a[:] = empty()
 >>> a
array([[empty class, empty class, empty class, empty class],
        [empty class, empty class, empty class, empty class],
        [empty class, empty class, empty class, empty class]], dtype=object)


or you may be able to some trick with strides that would give you 
zero-size elements, though I suppose you'd need at least one byte 
allocated for the data pointer.

Can you have an empty struct in C?

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list