Reputation: 779
I have a python list with strings in this format:
A1 = [' "29.0" ',' "65.2" ',' "75.2" ']
How do I convert those strings into decimal numbers to perform arithmetic operations on the list elements?
Upvotes: 77
Views: 295097
Reputation: 2419
If you are converting price (in string) to decimal price then....
from decimal import Decimal
price = "14000.45"
price_in_decimal = Decimal(price)
Upvotes: 26
Reputation: 1
There are two floating-point data types. Float
and Decimal
. It depends on your program demand which one you want to use. Decimal
provides a high level of accuracy and it's more precise than Float
.
To convert a value string to float just do it:
num = "29.0"
print (float(num))
To convert string to decimal
from decimal import Decimal
num = "29.0"
print (Decimal(num))
For your specific question, you can use the below code. Both are working for me.
from decimal import Decimal
import re
result = [float(x.strip(' "')) for x in A1]
print(result)
#[29.0, 65.2, 75.2]
result = [Decimal(x.strip(' "')) for x in A1]
print(result)
#[Decimal('29.0'), Decimal('65.2'), Decimal('75.2')]
result = [float(re.search(r'\d+.\d+',number).group()) for number in A1]
print(result)
#[29.0, 65.2, 75.2]
result = [Decimal(re.search(r'\d+.\d+',number).group()) for number in A1]
print(result)
#[Decimal('29.0'), Decimal('65.2'), Decimal('75.2')]
Upvotes: 0
Reputation: 26492
In Python there are two floating point datatypes Float
and Decimal
. The use case depends on the precision of decimal you want in your program. Float
is quick and Decimal
is precise.
To convert a string to a floating point number just do
import decimal
float('1.2')
decimal.Decimal('1.2')
Analysing Float
and Decimal
objects
>>> import sys
>>> from decimal import Decimal
>>> num = '1.2'
>>> float(num)
1.2
>>> Decimal(num)
Decimal('1.2')
>>> # Precision
>>> float(1.2)
1.2
>>> Decimal(1.2)
Decimal('1.1999999999999999555910790149937383830547332763671875')
>>> # Memory usage
>>> sys.getsizeof(Decimal(num))
104
>>> sys.getsizeof(float(num))
24
>>> # Performance
>>> %timeit float(num)
140 ns ± 2.27 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
>>> %timeit Decimal(num)
192 ns ± 9.42 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In your particular case you can use @Mark Byers solution.
Upvotes: 5
Reputation: 788
If you are converting string to float:
import re
A1 = [' "29.0" ',' "65.2" ',' "75.2" ']
float_values = [float(re.search(r'\d+.\d+',number).group()) for number in A1]
print(float_values)
>>> [29.0, 65.2, 75.2]
Upvotes: -1
Reputation: 839144
If you want the result as the nearest binary floating point number use float
:
result = [float(x.strip(' "')) for x in A1]
If you want the result stored exactly use Decimal
instead of float
:
from decimal import Decimal
result = [Decimal(x.strip(' "')) for x in A1]
Upvotes: 109
Reputation: 11022
A2 = [float(x.strip('"')) for x in A1]
works, @Jake , but there are unnecessary 0s
Upvotes: 1
Reputation: 8855
use the built in float() function in a list comprehension.
A2 = [float(v.replace('"','').strip()) for v in A1]
Upvotes: 2
Reputation: 13151
You will need to use strip()
because of the extra bits in the strings.
A2 = [float(x.strip('"')) for x in A1]
Upvotes: 3