Ellipsis usage?

Peter Otten __peter__ at web.de
Wed Feb 18 16:54:22 EST 2004


Wayne Folta wrote:

> I see Ellipsis documented under slicing, though it appears it's not
> used in slicing but rather slicing-like notation for dictionary keys.
> It's a minor thing but none of the books I have even mention it. Is it
> obsolete in python 2.x or still useful?

The Nutshell has it in the chapter about the Numeric package (but not in the
index), and Numeric is the only package I know of that uses the ellipsis:

>>> a = Numeric.reshape(range(8), (2,2,2))
>>> a
array([[[0, 1],
        [2, 3]],
       [[4, 5],
        [6, 7]]])
>>> a[1,1,1]
7
>>> a[:,:,1]
array([[1, 3],
       [5, 7]])

This can be abbreviated:

>>> a[...,1]
array([[1, 3],
       [5, 7]])

Another example:

>>> a[1,...,1]
array([5, 7])
>>> a[1,:,1]
array([5, 7])

I. e. you can specify the leading and the trailing dimension(s), and the
ellipsis inserts defaults for the intermediate dimensions.

Peter




More information about the Python-list mailing list