473,473 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting dictionary value from string to float

2 New Member
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 20369
bvdet
2,851 Recognized Expert Moderator Specialist
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
MortyContorty
2 New Member
Worked great! Thanks.

P.S. I must be green for naming a dictionary "data_list".....
Nov 16 '07 #3
bartonc
6,596 Recognized Expert Expert
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:...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.