Indexing list of lists

Lukasz Pankowski lupan at zamek.gda.pl
Thu Sep 18 04:26:00 EDT 2003


hildegarde_roth at yahoo.de (Hilde Roth) writes:

> While we are at it, I also don't understand why sequences can't be
> used as indices. Why not, say, l[[2,3]] or l[(2, 3)]? Why a special 
> slice concept? To me, it's not just the step argument in the slice
> that is overkill... 

1. It will be more typing and harder to visually parse

    l[:3] would be l[(0, 3)]
    l[3:] would be l[(3,-1)]

2. Slicing two dimensional object will not be possible as the notion
   you proposed is used just for that (ex. l[1,2] which is equivallent
   to l[(1,2)] see below), and Numeric and numarray use it. See what
   happens with an object which on indexing just returns the index

>>> class C:
...     def __getitem__(self, i): return i
... 
>>> c = C()
>>> c[3]
3
>>> c[:3]
slice(0, 3, None)
>>> # multi dimensional indexing
>>> c[1,3]
(1, 3)
>>> c[1:3,3:5]
(slice(1, 3, None), slice(3, 5, None))

-- 

=*= Lukasz Pankowski =*=




More information about the Python-list mailing list