[Tutor] Array slices in python with negative step

vishwajeet singh dextrous85 at gmail.com
Mon Jan 11 18:17:59 CET 2010


On Mon, Jan 11, 2010 at 10:32 PM, spir <denis.spir at free.fr> wrote:

> On Mon, 11 Jan 2010 21:50:10 +0530
> vishwajeet singh <dextrous85 at gmail.com> wrote:
>
> > Hello All,
> >
> > I am bit confuse how the slice works in case a negative step is supplied
> > a = 'abcde'
> > a[::-1] gives me edcba
> >
> > but [4:0:-1] gives me edcb
> >
> > while searching net I came accross something which said the following:
> >
> > If a negative stride is specified and the starting or stopping indices
> are
> > omitted, they default to ``end of axis'' and ``beginning of axis''
> > respectively.
> > In my case 4 is end of axis and 0 is begining of the axis so a[::-1]
> should
> > be equivalent to [4:0:-1] but result which I get in both the cases does
> not
> > validate.
>
> Your interpretation is wrong. In your case, 4 is start index, 0 is end
> index (as obviously shown by the result you get). Step -1 lets you step
> backwards, but this does not change the fact that the end-index is the
> second argument of the slice, right? Now, python work with (right-side)
> half-open intervals, meaning that the second border is excluded (s[0:4]
> would return "abcd"). Which means in your case you won't get a[0] in the
> result.

Hope I'm clear... It can be a bit misleading at start but, as it's
> consistent, once you get used to it all works fine ;-)
>

 Thanks for your response but I am still not clear; can you show me the
equivalent of [::-1]  in terms actually specifying start-index, end-index
which gives me reverse of the given arrary.



>
> Denis
> ________________________________
>
> la vita e estrany
>
> http://spir.wikidot.com/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Vishwajeet Singh
+91-9657702154 | dextrous85 at gmail.com | http://singhvishwajeet.com
Twitter: http://twitter.com/vishwajeets | LinkedIn:
http://www.linkedin.com/in/singhvishwajeet
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100111/5d2173f8/attachment.htm>


More information about the Tutor mailing list