neymar neymar - 23 days ago 7
Python Question

convert the value of dictionary from list to float

I have a dictionary named "location" like this:

{
'WA': [
'47.3917',
'-121.5708'
],
'VA': [
'37.7680',
'-78.2057'
],
...
}


I want to convert to a dic that the value is a float, so it looks like:

{
'WA': [
47.3917,
-121.5708
],
'VA': [
37.7680,
-78.2057
],
...
}


I tried

for key in location.keys():
location[key] = float(location[key][0,1])
print location


it gives me an arror that "float() argument must be a string or a number"

how can I fix that?

Udy Udy
Answer

Your problem is in here: float(location[key][0,1])

# float objects are length 1
for key in location.keys():
    location[key] = [float(location[key][0]),float(location[key][1])]
print location