convert the value of dictionary from list to float

前端 未结 3 1542
陌清茗
陌清茗 2021-01-21 11:00

I have a dictionary named \"location\" like this:

{
    \'WA\': [
        \'47.3917\',
        \'-121.5708\'
    ],
    \'VA\': [
        \'37.7680\',
        \'         


        
3条回答
  •  醉话见心
    2021-01-21 11:45

    You can use dictionary comprehension to construct a dictionary which has the values converted to floats, like this

    print {k:map(float, locations[k]) for k in locations}
    

    As suggested by @Grijesh in the comments section, if you are using Python 3,

    print({k:list(map(float, locations[k])) for k in locations})
    

提交回复
热议问题