[Tutor] is the the two style of writting the same?

tan Sikonai tanjyunda at gmail.com
Wed Aug 21 05:58:09 CEST 2013


Oh,I have just thinked that the max index equals the length of the list,
but python supports more index.


2013/8/21 eryksun <eryksun at gmail.com>

> On Tue, Aug 20, 2013 at 3:31 PM, Walter Prins <wprins at gmail.com> wrote:
> > On 20 August 2013 10:20, sikonai sikonai <tanjyunda at gmail.com> wrote:
> >>
> >> >>> list=[1,2,3,4,5,6,7,8,9]
> >> >>> list[9:0:-2]
> >> [9, 7, 5, 3]
> >> >>> list[10:0:-2]
> >> [9, 7, 5, 3]
> >
> > For the specific list you show, the 2 expressions yield the same result.
> > However this does not mean that this is true of any list.  The reason
> the 2
> > expressions return the same result is because the starting index for the
> > slice operation is effectively capped at 9, due to the list containing
> only
> > 9 elements, so hence any value above 9 in the first slice parameter is
> > essentially reset/capped to 9.
>
> The start index is capped at 8:
>
>     >>> slice(9, 0, -2).indices(9)
>     (8, 0, -2)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130821/c71f7094/attachment-0001.html>


More information about the Tutor mailing list