Extended slicing and Ellipsis - where are they used?

R

Rodney Maxwell

R

Robert Kern

Rodney said:
The following are apparently legal Python syntactically:
L[1:3, 8:10]
L[1, ..., 5:-2]

But they don't seem to work on lists:
l = [0,1,2,3]
l[0:2,3]
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

So where is this extended slicing used?

numpy for multidimensional arrays.

http://numpy.scipy.org/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

James Stroud

Rodney said:
The following are apparently legal Python syntactically:
L[1:3, 8:10]
L[1, ..., 5:-2]

But they don't seem to work on lists:
l = [0,1,2,3]
l[0:2,3]
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers

So where is this extended slicing used?

AFAICT this syntax is not used in the standard library. However, the
mega-beauty of it is that you can make use of it in your own classes:

py> class Bob(list):
.... def __getitem__(self, i):
.... try:
.... return [list.__getitem__(self, j) for j in i]
.... except TypeError:
.... return list.__getitem__(self, i)
....
py> b = Bob(xrange(15, 30))
py> b[3, 5, 7, 13]
[18, 20, 22, 28]

James
 
P

Paddy

Rodney said:
The following are apparently legal Python syntactically:
L[1:3, 8:10]
L[1, ..., 5:-2]
But they don't seem to work on lists:
l = [0,1,2,3]
l[0:2,3]
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
So where is this extended slicing used?

AFAICT this syntax is not used in the standard library. However, the
mega-beauty of it is that you can make use of it in your own classes:

py> class Bob(list):
... def __getitem__(self, i):
... try:
... return [list.__getitem__(self, j) for j in i]
... except TypeError:
... return list.__getitem__(self, i)
...
py> b = Bob(xrange(15, 30))
py> b[3, 5, 7, 13]
[18, 20, 22, 28]

James

And the ellipses ... ?

- Paddy.
 
J

James Stroud

Paddy said:
And the ellipses ... ?

;)


py> class Bob(dict):
.... def __getitem__(self, k, *args, **kwargs):
.... if k is Ellipsis:
.... return sorted(self.keys())
.... else:
.... return dict.__getitem__(self, k, *args, **kwargs)
.... def __setitem__(self, k, *args, **kwargs):
.... if k is Ellipsis:
.... raise KeyError, "Can't make elliptical assignments."
.... else:
.... return dict.__setitem__(self, k, *args, **kwargs)
....
py> b = Bob(a=1, b=2, c=3, d=15.5)
py> b
{'a': 1, 'b': 2, 'c': 3, 'd': 15.5}
py> for k in b[...]:
print '%s ==> %s' % (k, b[k])
....
a ==> 1
b ==> 2
c ==> 3
d ==> 15.5
py> b[...] = 2
 
P

Paddy

Paddy said:
And the ellipses ... ?

;)

py> class Bob(dict):
... def __getitem__(self, k, *args, **kwargs):
... if k is Ellipsis:
... return sorted(self.keys())
... else:
... return dict.__getitem__(self, k, *args, **kwargs)
... def __setitem__(self, k, *args, **kwargs):
... if k is Ellipsis:
... raise KeyError, "Can't make elliptical assignments."
... else:
... return dict.__setitem__(self, k, *args, **kwargs)
...
py> b = Bob(a=1, b=2, c=3, d=15.5)
py> b
{'a': 1, 'b': 2, 'c': 3, 'd': 15.5}
py> for k in b[...]:
print '%s ==> %s' % (k, b[k])
...
a ==> 1
b ==> 2
c ==> 3
d ==> 15.5
py> b[...] = 2
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File "<ipython console>", line 9, in __setitem__
<type 'exceptions.KeyError'>: "Can't make elliptical assignments."

Thanks again James.
 
R

Rodney Maxwell

Rodney said:
The following are apparently legal Python syntactically:
L[1:3, 8:10]
L[1, ..., 5:-2]
But they don't seem to work on lists:
l = [0,1,2,3]
l[0:2,3]
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
So where is this extended slicing used?

AFAICT this syntax is not used in the standard library. However, the
mega-beauty of it is that you can make use of it in your own classes:

py> class Bob(list):
... def __getitem__(self, i):
... try:
... return [list.__getitem__(self, j) for j in i]
... except TypeError:
... return list.__getitem__(self, i)
...
py> b = Bob(xrange(15, 30))
py> b[3, 5, 7, 13]
[18, 20, 22, 28]

James
Or
[[15, 16, 17], 20, 24]

which is what I was looking for in the first place.

Thanks,
Rodney
 
R

Rodney Maxwell

Rodney said:
The following are apparently legal Python syntactically:
L[1:3, 8:10]
L[1, ..., 5:-2]
But they don't seem to work on lists:
l = [0,1,2,3]
l[0:2,3]
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers
So where is this extended slicing used?

AFAICT this syntax is not used in the standard library. However, the
mega-beauty of it is that you can make use of it in your own classes:

py> class Bob(list):
... def __getitem__(self, i):
... try:
... return [list.__getitem__(self, j) for j in i]
... except TypeError:
... return list.__getitem__(self, i)
...
py> b = Bob(xrange(15, 30))
py> b[3, 5, 7, 13]
[18, 20, 22, 28]

James
Or
[[15, 16, 17], 20, 24]

which is what I was looking for in the first place.

Thanks,
Rodney
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top