Question about idioms for clearing a list

bonono at gmail.com bonono at gmail.com
Tue Feb 7 20:48:46 EST 2006


Tim Hochberg wrote:
> bonono at gmail.com wrote:
> > Fredrik Lundh wrote:
> >
> >>>Python now has, what, three built-in mutable collections types:
> >>>lists, dictionaries, and sets. Dicts and sets both have a clear()
> >>>method and lists do not.
> >>
> >>dicts and sets are mappings, and lists are not.  mappings don't
> >>support slicing.  lists do.
> >
> >
> > I am confused. Could you explain this ? I was under the impression said
> > above(mapping don't support slicing), until after I read the language
> > reference. I don't think it is slicing as in the list slicing sense but
> > it does use the term "extend slicing".
> >
> > http://www.python.org/doc/2.4.2/ref/slicings.html
> >
> > "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 3.2) 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."
>
>
> This is in place to support multidimensional arrays, such as in numpy.
> If, for instance, you have a 9x9 array A, then A[3:6,3:6] will extract a
> 3x3 block from the center of it. A[3:6,3:6] is equivalent to
> A[(slice(3,6,None), slice(3,6,None))] and the resulting tuple gets
> passed through the mapping interface, but it is not a mapping in any
> real sense.
>
> I don't think there's anything in core Python that uses this and it's
> not really relevant to this thread.
>
Thanks. I would say that it is not relevant to the OP's question but
the thread has turned to "anyone who read the doc should know about
slicing" and that prompted me to go and read the doc(I don't have the
OP's mentioned need in my coding so far and never read about the full
pontential of slicing, just use it as the right hand side intuitively)
about slicing and found the documentation to be a bit lacking(only a
brief mentioning of slicing in the turtorial and this confusing
section).

Beside, just from reading this page, it seems that a[start:stop:stride]
is not a valid construction for sequence object 'a'.

To me, reading the doc makes me more confuse about what is slicing.




More information about the Python-list mailing list