Ellipsis usage?

Discussion in 'Python' started by Wayne Folta, Feb 18, 2004.

  1. Wayne Folta

    Wayne Folta Guest

    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?
     
    Wayne Folta, Feb 18, 2004
    #1
    1. Advertising

  2. On Wed, 18 Feb 2004 11:39:04 -0500, 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?
    >
    >


    Hello,

    I have used Ellipsis in a project where the ... notation was more
    adapted than : (because x:y had another meaning in our context).

    I had a class similar to this one:

    class MyRange:

    def __init__(self, lst):
    self.lst = lst

    def __getitem__(self, slice):
    r = []
    items = list(slice)
    while items:
    a = items.pop(0)
    if items[:1] == [Ellipsis]:
    items.pop(0)
    b = items.pop(0)
    r.extend(self.lst[a:b+1])
    else:
    r.append(self.lst[a])
    return r

    r = MyRange(list("abcdefghijklmnopqrstuvwxyz"))
    print r[1, 2, 3, 10, ..., 15]

    should print ['b', 'c', 'd', 'k', 'l', 'm', 'n', 'o', 'p']


    But I don't know exactly what's the real use of Ellipsis and if it will
    exist in the future.
     
    Christophe Delord, Feb 18, 2004
    #2
    1. Advertising

  3. Wayne Folta

    Peter Otten Guest

    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
     
    Peter Otten, Feb 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    2
    Views:
    540
    Hans Kesting
    Feb 14, 2006
  2. Chris Perkins

    Ellipsis outside a slice

    Chris Perkins, Oct 8, 2003, in forum: Python
    Replies:
    1
    Views:
    954
    Terry Reedy
    Oct 9, 2003
  3. =?iso-8859-1?q?Jonas_K=F6lker?=

    !wtf ellipsis

    =?iso-8859-1?q?Jonas_K=F6lker?=, Aug 13, 2004, in forum: Python
    Replies:
    6
    Views:
    587
    Erik Max Francis
    Aug 15, 2004
  4. Jerry Sievers

    slicing, mapping types, ellipsis etc.

    Jerry Sievers, Nov 29, 2004, in forum: Python
    Replies:
    9
    Views:
    375
    Kent Johnson
    Nov 30, 2004
  5. =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=

    doctest's ELLIPSIS

    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=, Apr 29, 2005, in forum: Python
    Replies:
    5
    Views:
    882
    =?iso-8859-1?q?S=E9bastien_Boisg=E9rault?=
    May 2, 2005
Loading...

Share This Page