[Numpy-discussion] how to create an array of objects that are sequences?

Christopher Barker Chris.Barker@noaa....
Sun Jan 6 02:01:27 CST 2008


Robert Kern wrote:
> Chris Barker wrote:
>> What if your "objects" were nested sequences, and you wanted to partly 
>> flatten them -- which would you flatten?
> 
> I'm pretty sure that that is exactly the ambiguity that the depth option
> resolves. Can you give me an example where it's still ambiguous with the depth
> information provided?

I was imagining:

Say you have a bunch of nested sequences that, fully expanded, would 
yield a (i,j,k) rank-3 array. If you wanted a rank-2 array, you could 
have either:
a (i,j) array with each element being a length-k sequence
or:
a (i,k) array, with each element being a length-j sequence.

This is quite trivial with numpy n-d arrays.

However, while you could build either of those from nested sequences, 
there isn't, in fact, and object in there that is that length-j sequence.

I guess this really points to the power of n-d arrays!

Here's a simple example:

t = (((1,2),(3,4)),((5,6),(7,8)))

depth 1 -- (4,):
array([(1, 2), (3, 4), (5, 6), (7, 8)], dtype=object)
 >>> a[0]
(1, 2)

depth 1 -- (2,)
 >>> a
array([(1, 2, 3, 4), (5, 6, 7, 8)], dtype=object)
 >>> a[0]
(1, 2, 3, 4)

But since there was no object in the original sequence that actually had 
a 4-tuple, maybe no one would ever be looking for that.

-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


More information about the Numpy-discussion mailing list