1

What's a simple way to convert stings into numerics:
from:

['3.65e+03', '1.14e+04', '1.35e+04', '1.46e+04']

to:

[3.65e+03, 1.14e+04, 1.35e+04, 1.46e+04]

Thanks.

3 Answers 3

4

First off, I hope you are aware of the limitations in representing the floating point accurately in our current architectures.

The simple way for conversion is simply this.

>>> s = ['3.65e+03', '1.14e+04', '1.35e+04', '1.46e+04']
>>> map(float,s) 
>>> [3650.0, 11400.0, 13500.0, 14600.0]

But float rounds them off to the nearest value and it's representation does not matter as long as values are same. Sometimes, those values can have near-equal representation with same value (almost) for e.g.

>>> s = ['3.65e+93', '1.14e+04', '1.35e+04', '1.46e+04']
>>> map(float,s)
[3.6500000000000001e+93, 11400.0, 13500.0, 14600.0]

Update - Please see John Machin's comment for this behavior/repr.

But if you want exact representation, for e.g. if you are dealing calculations involving money, then you may want to use the Decimal type instead of float, which for all purposes can behave in the same way as your float.

>>> from decimal import Decimal
>>> map(Decimal,s)
[Decimal('3.65E+93'), Decimal('1.14E+4'), Decimal('1.35E+4'), Decimal('1.46E+4')]
Sign up to request clarification or add additional context in comments.

3 Comments

-1 You are confused: float() ALWAYS rounds to the nearest float value, if necessary. "As you saw"?? There is NO ROUNDOFF involved in your first map() output. In your second, 3.6500000000000001e+93 is merely an artifact of your use of a pre-2.7 Python, where repr() always used 17 decimal digits of precision. Python 2.7 produces 3.65e+93. In ANY Python, 3.6500000000000001e+93 == 3.65e+93 is true. Very shaky grounds for introducing decimal which has its own limited precision.
John - Thanks for the explanation and correction. I have updated few sections in my answer. As for Decimal's precision, it is controllable using getcontext().prec =n isn't it?
Yes the decimal precision is controllable; there are lots of knobs/buttons/switches that the novice can turn/push/flip. Your answer is still confused. (1) delete "(almost)" (2) If decimal behaves the same as float, why use it?
4
map(float, your_list)

or

[float(x) for x in your_list]

Update For an explanation of why an unqualified recommendation to "use decimal" is not a good idea, see my answer to another question.

1 Comment

+1 for the list comprehension -- usually more common in Python, plus allows an if clause when required. BTW, did you intend to link to this answer?
-1
numbers = [eval(s) for s in ['3.65e+03', '1.14e+04', '1.35e+04', '1.46e+04']]

eval's a bit of a hack, but it's pretty well guaranteed to work. (specific float is also a good approach.)

1 Comment

-1: Presumably the strings are coming from an external source (command line, file, network etc). Blindly using eval() is a huge risk as it allows arbitrary code to be executed. Even discounting this security risk, why would you want to use eval() over the in-built float() function which was designed specifically for this purpose?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.