Countdown
Tim Hochberg
tim.hochberg at ieee.org
Thu Jun 22 19:12:47 EDT 2000
Jeff Sandys <sandysj at asme.org> writes:
> Python is really elegant when counting up:
>
> for i in range(len(thing)):
> print thing[i]
>
> But when counting down looks ugly:
>
> for j in range(len(thing)-1,-1,-1):
> print thing[i]
>
> Advice so far:
> Stop doing things backwards:
> (customer request)
> Use reverse first:
> (destructive, need to unreverse after)
> Live with it:
> (bytecode doesn't care, but it is still ugly)
> Try range(len(thing)).reverse():
> (doesn't work but):
> rthing = range(len(thing))
> rthing.reverse()
> for k in rthing:
> print thing[i]
> (works, but also ugly)
This is completely overkill for this, but you could get Numeric and use:
from Numeric import arange
for j in arange(len(thing))[::-1]:
print thing[j]
-tim
More information about the Python-list
mailing list