[Tutor] confusing enumerate behavior
Kent Johnson
kent37 at tds.net
Fri Feb 6 12:54:49 CET 2009
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
More information about the Tutor
mailing list