Converting dictionary value from string to float

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MortyContorty
    New Member
    • Nov 2007
    • 2

    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.
    [CODE=python]
    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'}[/CODE]

    Any help would be appreciated!
    Last edited by bartonc; Nov 16 '07, 04:22 AM. Reason: Added [CODE=python][/CODE] tags.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by MortyContorty
    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[code=Python]newDict = dict(zip(data_l ist.keys(), [float(value) for value in data_list.value s()][/code]

    Comment

    • MortyContorty
      New Member
      • Nov 2007
      • 2

      #3
      Worked great! Thanks.

      P.S. I must be green for naming a dictionary "data_list".... .

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by MortyContorty
        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.
        [CODE=python]
        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'}[/CODE]

        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:[CODE=python]newDict = {} # an empty dictionary
        for key, value in data_list.items (): # get the (key, value) tuples one at a time
        newDict[key] = value[/CODE]

        Comment

        Working...