question about a data structure
Hello, Suppose I want to store some data (a non-negative integer) related to an "event" involving two entities. It occurs to me that the way to do it is using a 2-d array, like a table: For example: a b c a 1 b 2 0 c 5 in the event 'b-a' the data is 2, and in the event 'c-a' is 5, etc. Now, I need to be able to differentiate between 0 and 'no data'. I know I could use a number like -1, but this would affect arithmetic operations on the array, and also I need that indexing one such location with no data returns an empty array. Is it possible to do this with the standard array class? Thanks. Ernest
2009/7/29 Ernest Adrogué
Hello,
Suppose I want to store some data (a non-negative integer) related to an "event" involving two entities. It occurs to me that the way to do it is using a 2-d array, like a table:
For example:
a b c a 1 b 2 0 c 5
in the event 'b-a' the data is 2, and in the event 'c-a' is 5, etc.
Now, I need to be able to differentiate between 0 and 'no data'. I know I could use a number like -1, but this would affect arithmetic operations on the array, and also I need that indexing one such location with no data returns an empty array.
Is it possible to do this with the standard array class?
Not really. Use masked arrays. Or use a dictionary. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Robert Kern wrote:
2009/7/29 Ernest Adrogué
: Now, I need to be able to differentiate between 0 and 'no data'.
Is it possible to do this with the standard array class?
Not really. Use masked arrays. Or use a dictionary.
masked arrays are made precisely for this. However, if you can use floating point numbers, you could use NaN for the no data entries. -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@noaa.gov
29/07/09 @ 14:54 (-0700), thus spake Christopher Barker:
Robert Kern wrote:
2009/7/29 Ernest Adrogué
: Now, I need to be able to differentiate between 0 and 'no data'.
Is it possible to do this with the standard array class?
Not really. Use masked arrays. Or use a dictionary.
masked arrays are made precisely for this. However, if you can use floating point numbers, you could use NaN for the no data entries.
Thanks for the advice. Actually, I think I'll use a combination of a dictionary and a masked array. I have been playing around a bit and it doesn't seem too difficult to do. It works by adding an extra property to the array objects, which is a tuple with the "keys", and then use the __getitem__ and __setitem__ methods to translate the keys into numbers. This way it's possible to use strings in subscripts and slices. Ernest
participants (3)
-
Christopher Barker
-
Ernest Adrogué
-
Robert Kern