number ranges (was Re: Matlab page on scipy wiki)

Tim Hochberg tim.hochberg at ieee.org
Sun Feb 19 18:28:12 EST 2006


Colin J. Williams wrote:

>>
>>It would be good if the range and slice could be merged in some way,
>>although the extended slice is rather complicated - I don't understand it.
>>
>>   The semantics for an extended slicing are as follows. The primary
>>   must evaluate to a mapping object, and it is indexed with a key that
>>   is constructed from the slice list, as follows. If the slice list
>>   contains at least one comma, the key is a tuple containing the
>>   conversion of the slice items; otherwise, the conversion of the lone
>>   slice item is the key. The conversion of a slice item that is an
>>   expression is that expression. The conversion of an ellipsis slice
>>   item is the built-in |Ellipsis| object. The conversion of a proper
>>   slice is a slice object (see section section 4.2 The standard type
>>   hierarchy <http://www.network-theory.co.uk/docs/pylang/ref_30.html>)
>>   whose |start|, |stop| and |step| attributes are the values of the
>>   expressions given as lower bound, upper bound and stride,
>>   respectively, substituting |None| for missing expressions.
>>
>>[source: http://www.network-theory.co.uk/docs/pylang/ref_60.html]
>>
>>The seems to be a bit of a problem with slicing that needs sorting out.
>>
>>The syntax for a slice list appears to allow multiple slices in a list:
>>
>>   extended_slicing     ::=     primary <primaries.html#tok-primary> "["
>>   slice_list <slicings.html#tok-slice_list> "]"
>>   slice_list     ::=     slice_item <slicings.html#tok-slice_item> (","
>>   slice_item <slicings.html#tok-slice_item>)* [","]
>>
>>but the current interpreter reports an error:
>>
>>    >>> a= range(20)
>>    >>> a[slice(3, 9, 2)]
>>   [3, 5, 7]
>>    >>> a[slice(3, 9, 2), slice(5, 10)]
>>   Traceback (most recent call last):
>>     File "<interactive input>", line 1, in ?
>>   TypeError: list indices must be integers
>>    >>>
>>

Extended slicing has nothing to do with lists. All that gobbeldy gook is 
trying to tell you is what the interpreter does with O[1:2:3, 4:5:6] 
where O is some arbitrary object. What it does is:

O[1:2:3, 4:5:6] == O[slice(1,2,3), slice(4,5,6)]
                 == O.__getitem__((slice(1,2,3), slice(4,5,6)))

In the case of python lists, __getitem__ doesn't support multiple slice 
arguments. However, you are free to define your own types which do 
support multiple slices.

This type of slicing was added to support Numeric originally and as far 
as I know is still only really used in Numeric and its successors 
Numarray and Numpy. Since this was originally posted to the numpy list I 
assume you are familiar with multidimensional indexing of arrays -- that 
is extended slicing in action.

If you really want to learn about it, and for most people it's 
unnecessary although perhaps entertaining, play with the following class:


class ExtendedSlicingDemo(object):
     def __getitem__(self, key):
         return key

esd = ExtendedSlicingDemo()

print esd[1:2:3]
print esd[1:2:3, 3:4:5]
print esd[1:2:3, ..., 3:4:5]
#....

=>

slice(1, 2, 3)
(slice(1, 2, 3), slice(3, 4, 5))
(slice(1, 2, 3), Ellipsis, slice(3, 4, 5))

-tim




More information about the Python-list mailing list