473,226 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 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 20308
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

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

Similar topics

1
by: Meya-awe | last post by:
Hello there, I am using a 3rd party library and a function which requires a float value in one of its methods. My data is in a string format and i tried using Convert.ToDouble and...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
5
by: vivekaseeja | last post by:
Hi , Trying to convert a string value to a float value after reading the value from an XML file , but not sure what function to use. The following only works for integers Int32.Parse...
3
by: Jim Langston | last post by:
I have a CSkill class which is rather complex as it is recursive. That is: class CSkill { public: CSkill( std::string Name, float Value ): Name_( Name ), Value_( Value ) {}; void Update(...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
3
by: dean.elwood | last post by:
Hi guys, My first post here and I'm a pascal coder doing his best to move to C/C++ so please go easy on me ;) I'm using the MySQL C API to pull some values from a DB. One of the fields is a...
3
by: Sharon | last post by:
There is an built-in types table for .NET framework type to C# type which are aliases of predefined types in the System namespace (http://msdn2.microsoft.com/en-US/library/ya5y69ds.aspx). How...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
0
by: Gabriel Genellina | last post by:
En Fri, 19 Sep 2008 10:59:26 -0300, Ron Brennan <brennan.ron@gmail.com> escribió: I guess you probably tried using ' '.join(value) and got an error like this: TypeError: sequence item 0:...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.