Python Convert Decimal Fixed Point List to Radians

573 views Asked by At

I have a list of decimal fixed point numbers:

latitude = Places.query.with_entities(Places.latitude).all()
result = []
for i in range(len(latitude)):
    result.append(latitude[i][0])
print result

The output of latitude is this I wanted to map them to Radians. So, I did this:

lat_ = map(lambda i: radians(i), result)

But got an errorTypeError: a float is required

I want to know what is the correct way to do this operation. `

Edit

Now the result looks like this:

[28.633, 29.333,...]

And error is:

TypeError: float() argument must be a string or a number
3

There are 3 answers

2
Bill On BEST ANSWER

Try this:

rads = [x['latitude'] for x in result]
_lat = map(lambda i: radians(i), rads)
1
Timo.S On

The problem is that the radians function from the maths module expects an input of type float, not of type Decimal. You'll have to convert the values to float first:

lat_ = map(lambda i: radians(float(i)), result)
0
AndersP On
from decimal import *

from math import radians

result = [Decimal('77.216700'), Decimal('77.250000'), Decimal('77.216700'), Decimal('77.216700'), Decimal('77.200000'), Decimal('77.216700')]

lat_ = map(lambda i: radians(float(i)), result)

# [1.3476856525247056, 1.3482668471656196, 1.3476856525247056,
#  1.3476856525247056, 1.3473941825396225, 1.3476856525247056]