iterating in reverse
Neil Hodgson
nhodgson at bigpond.net.au
Sat Jan 18 15:25:46 EST 2003
Steve Holden:
> Here's my own non-reversing solution. Again, it would need tweaking for
> negative numbers to avoid results like "-,123".
>
> def commafy(val):
> ...
Well we may as well try a one liner (almost):
def commafy(val):
u = str(val)
return ''.join([u[i]+(['',',',''][(len(u)-i)%3]) for i in
range(len(u))])[:-1]
for i in [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678]:
print i, ":", commafy(i)
Mmm, Perl envy ...
Neil
More information about the Python-list
mailing list