I'm looking for a way to convert numbers to string format, dropping any redundant '.0'
The input data is a mix of floats and strings. Desired output:
0 --> '0'
0.0 --> '0'
0.1 --> '0.1'
1.0 --> '1'
I've come up with the following generator expression, but I wonder if there's a faster way:
(str(i).rstrip('.0') if i else '0' for i in lst)
The truth check is there to prevent 0 from becoming an empty string.
EDIT: The more or less acceptable solution I have for now is this:
('%d'%i if i == int(i) else '%s'%i for i in lst)
It just seems strange that there is no elegant way to handle this (fairly straightforward) case in python.
hash()seems to be a little faster thanint(), so you might usehash(i)==i, I'm not sure it's safe though - saeedgnuhashinstead ofint- Antony Hatchkinshash(2**64) == 8for example - Francisco C