[Numpy-discussion] How to get the shape of an array slice without doing it

josef.pktd at gmail.com josef.pktd at gmail.com
Fri Jan 29 13:27:24 EST 2010


On Fri, Jan 29, 2010 at 1:03 PM, Keith Goodman <kwgoodman at gmail.com> wrote:
> On Fri, Jan 29, 2010 at 9:53 AM,  <josef.pktd at gmail.com> wrote:
>> I forgot about ellipsis, since I never use them,
>> replace ellipsis by [slice(None)]*ndim or something like this
>>
>> I don't know how to access an ellipsis directly, is it even possible
>> to construct an index list that contains an ellipsis?
>> There is an object for it but I never looked at it.
>
> I haven't been following the discussion and I don't understand your
> question and in a moment I will accidentally hit send...
>
>>> class eli(object):
>   ...:
>   ...:         def __init__(self):
>   ...:             pass
>   ...:
>   ...:     def __getitem__(self, index):
>   ...:             print index
>   ...:
>
>>> x[...]
> Ellipsis
>>> x[...,1]
> (Ellipsis, 1)
>
> Ellipsis is a python class. Built in, no need to import.

thanks, this makes it possible to construct index lists with Ellipsis,
but it showed that my broadcast idea doesn't work this way

Travis explained last year how slices and broadcasting are used for
indexing, and it's quite a bit more complicated than this.

Sorry for jumping in too fast.

Josef

>>> indi= (slice(2,5),Ellipsis, np.arange(3)[:,None])
>>> ind2 = []
>>> for i in indi:
	if not i is Ellipsis: ind2.append(i)
	else: ind2.extend([slice(None)]*2)

	
>>> ind2
[slice(2, 5, None), slice(None, None, None), slice(None, None, None),
array([[0],
       [1],
       [2]])]
>>> np.broadcast(*ind2).shape
(3, 1)



> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list