Ellipsis usage?

W

Wayne Folta

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?
 
C

Christophe Delord

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.
 
P

Peter Otten

Wayne said:
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:
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
array([[1, 3],
[5, 7]])

This can be abbreviated:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top