[Tutor] confusing enumerate behavior

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Fri Feb 6 14:52:52 CET 2009


On Fri, Feb 6, 2009 at 6:54 AM, Kent Johnson <kent37 at tds.net> wrote:

> 2009/2/6 jitendra gupta <jitu.icfai at gmail.com>:
>
> > Try this if u r looking for this kind of solution
> >>>>my_input = "one two three four five six seven eight nine ten"
> >>>>text = my_input.split()
> >>>>for i in range(len(text)):
> >        if i+3>=len(text):
> >                print text[i-3:len(text):1]
> >        elif i<=2:
> >                print text[0:i+4]
> >        else:
> >                print text[i-3:i+4]
>
> You can simplify this using the min() and max() functions:
> for i in range(len(text)):
>     print text[max(0, i-3):min(i+3, len(text))]
>
> Kent
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


Thank you all for the great solutions. I'm really grateful, but also still
confused about indexing now. Why is it that if i = 0 indexing starts with
-3.  I never read this before, and I thus assume it is an advanced thing, or
why is it not in elementary to intermediate resources ( The ones I used at
least). Can somebody please suggest an accessible tutorial on this?
-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington

--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090206/5ed9d482/attachment-0001.htm>


More information about the Tutor mailing list