[Numpy-discussion] ragged array implimentation
Sturla Molden
sturla at molden.no
Mon Mar 7 20:51:55 EST 2011
Den 07.03.2011 18:28, skrev Christopher Barker:
> 1, 2, 3, 4
> 5, 6
> 7, 8, 9, 10, 11, 12
> 13, 14, 15
> ...
>
> In my case, these will only be 2-d, though I suppose one could have a
> n-d version where the last dimension was ragged (or any dimension, I
> suppose, though I'm having trouble wrapping my brain around what that
> would look like..
A ragged array, as implemented in C++, Java or C# is just an array of
arrays (or 'a pointer to an array of pointers'). Basically, that is an
ndarray of ndarrays (or a list of ndarrays, whatever you prefer).
>>> ra = np.zeros(4, dtype=np.ndarray)
>>> ra[0] = np.array([1,2,3,4])
>>> ra[1] = np.array([5,6])
>>> ra[2] = np.array([7,8,9,10,11,12])
>>> ra[3] = np.array([13,14,15])
>>> ra
array([[1 2 3 4], [5 6], [ 7 8 9 10 11 12], [13 14 15]], dtype=object)
>>> ra[1][1]
6
>>> ra[2][:]
array([ 7, 8, 9, 10, 11, 12])
Slicing in two dimensions does not work as some might expect:
>>> ra[:2][:2]
array([[1 2 3 4], [5 6]], dtype=object)
This "strange" result would actually not be different from the ragged
array semantics of C++, C# or Java if those languages had supported slicing.
Indexing [::][::] instead of [::,::] corresponds to what you must do in
C#, C++ and Java as well. (We are doing exactly the same, so why should
it not be?)
Sturla
More information about the NumPy-Discussion
mailing list