Changing Data Type of Values in Dictionaries [duplicate]












0
















This question already has an answer here:




  • convert the value of dictionary from list to float

    3 answers




I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.



My code is:



d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}


Whereas I would like:



d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}


These values are meant to be stored as floats as some division follows later










share|improve this question













marked as duplicate by jpp dictionary
Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 16:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • convert the value of dictionary from list to float

      3 answers




    I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.



    My code is:



    d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}


    Whereas I would like:



    d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}


    These values are meant to be stored as floats as some division follows later










    share|improve this question













    marked as duplicate by jpp dictionary
    Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Jan 1 at 16:48


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0


      1







      This question already has an answer here:




      • convert the value of dictionary from list to float

        3 answers




      I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.



      My code is:



      d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}


      Whereas I would like:



      d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}


      These values are meant to be stored as floats as some division follows later










      share|improve this question















      This question already has an answer here:




      • convert the value of dictionary from list to float

        3 answers




      I have a program which reads a file and converts the contents into a dictionary (d). The values all come in as a string. The value of each key is a list containing two elements. Both of these elements are numbers and I wish to store them as floats. I'm having a hard time finding how I can access this specific part of each value-list in the dictionary to change it.



      My code is:



      d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], ... 'mango':['2.0','431.0']}


      Whereas I would like:



      d = {'apples':[100.0, 23.5], 'bananas':[41.5, 321.0], ... 'mango':[2.0, 431.0]}


      These values are meant to be stored as floats as some division follows later





      This question already has an answer here:




      • convert the value of dictionary from list to float

        3 answers








      python list dictionary nested






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 16:46









      MattMatt

      308




      308




      marked as duplicate by jpp dictionary
      Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 1 at 16:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by jpp dictionary
      Users with the  dictionary badge can single-handedly close dictionary questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Jan 1 at 16:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Use this code



          d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
          dict={}
          for i in d:
          dict[i]=
          for j in d[i]:
          dict[i].append(float(j))


          print(dict)


          It will generate new dictionary which values are float type






          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Use this code



            d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
            dict={}
            for i in d:
            dict[i]=
            for j in d[i]:
            dict[i].append(float(j))


            print(dict)


            It will generate new dictionary which values are float type






            share|improve this answer




























              0














              Use this code



              d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
              dict={}
              for i in d:
              dict[i]=
              for j in d[i]:
              dict[i].append(float(j))


              print(dict)


              It will generate new dictionary which values are float type






              share|improve this answer


























                0












                0








                0







                Use this code



                d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
                dict={}
                for i in d:
                dict[i]=
                for j in d[i]:
                dict[i].append(float(j))


                print(dict)


                It will generate new dictionary which values are float type






                share|improve this answer













                Use this code



                d = {'apples':['100.0','23.5'], 'bananas':['41.5','321.0'], 'mango':['2.0','431.0']}
                dict={}
                for i in d:
                dict[i]=
                for j in d[i]:
                dict[i].append(float(j))


                print(dict)


                It will generate new dictionary which values are float type







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 16:54









                Mohammad AnsariMohammad Ansari

                1131315




                1131315

















                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith