471,348 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Converting dictionary value from string to float

Hello:

I am new to Python (and a programming greenie for that matter) and I was trying to figure out how to change the "value" of my dictionary dataset from a string to a float. I imagine it is easy, but I haven't been able to figure it out.
Expand|Select|Wrap|Line Numbers
  1. data_list ={'11': '0.0', '10': '0.0', '13': '0.0', '12': '10.0', '15': '10.0', '14': '10.0', '17': '10.0', '16': '10.0', '19': '10.0', '18': '10.0', '20': '10.0', '1': '10.0', '3': '0.0', '2': '0.0', '5': '0.0', '4': '0.0', '7': '0.0', '6': '0.0', '9': '0.0', '8': '0.0', '21': '10.0'}
Any help would be appreciated!
Nov 16 '07 #1
3 19551
bvdet
2,851 Expert Mod 2GB
Hello:

I am new to Python (and a programming greenie for that matter) and I was trying to figure out how to change the "value" of my dictionary dataset from a string to a float. I imagine it is easy, but I haven't been able to figure it out.

data_list ={'11': '0.0', '10': '0.0', '13': '0.0', '12': '10.0', '15': '10.0', '14': '10.0', '17': '10.0', '16': '10.0', '19': '10.0', '18': '10.0', '20': '10.0', '1': '10.0', '3': '0.0', '2': '0.0', '5': '0.0', '4': '0.0', '7': '0.0', '6': '0.0', '9': '0.0', '8': '0.0', '21': '10.0'}

Any help would be appreciated!
This should do it
Expand|Select|Wrap|Line Numbers
  1. newDict = dict(zip(data_list.keys(), [float(value) for value in data_list.values()]
Nov 16 '07 #2
Worked great! Thanks.

P.S. I must be green for naming a dictionary "data_list".....
Nov 16 '07 #3
bartonc
6,596 Expert 4TB
Hello:

I am new to Python (and a programming greenie for that matter) and I was trying to figure out how to change the "value" of my dictionary dataset from a string to a float. I imagine it is easy, but I haven't been able to figure it out.
Expand|Select|Wrap|Line Numbers
  1. data_list ={'11': '0.0', '10': '0.0', '13': '0.0', '12': '10.0', '15': '10.0', '14': '10.0', '17': '10.0', '16': '10.0', '19': '10.0', '18': '10.0', '20': '10.0', '1': '10.0', '3': '0.0', '2': '0.0', '5': '0.0', '4': '0.0', '7': '0.0', '6': '0.0', '9': '0.0', '8': '0.0', '21': '10.0'}
Any help would be appreciated!
My friend bvdet has a great solution, but all those () and [] can be a bit daunting for a beginner. To make it look simpler, let's break it in its basic parts:
Expand|Select|Wrap|Line Numbers
  1. newDict = {} # an empty dictionary
  2. for key, value in data_list.items(): # get the (key, value) tuples one at a time
  3.     newDict[key] = value
Nov 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Meya-awe | last post: by
1 post views Thread by john wright | last post: by
5 posts views Thread by vivekaseeja | last post: by
3 posts views Thread by Jim Langston | last post: by
3 posts views Thread by dean.elwood | last post: by
10 posts views Thread by Hank Stalica | last post: by
reply views Thread by Gabriel Genellina | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.