On Fri, Feb 6, 2009 at 6:54 AM, Kent Johnson <ken...@tds.net> wrote: > 2009/2/6 jitendra gupta <jitu.ic...@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@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 --------------------------------------------------------
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor