Turning a string of integers into a list and sorting it by odd and even elements











up vote
1
down vote

favorite












I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.



I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.



I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help










share|improve this question


















  • 2




    But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
    – jpp
    Nov 11 at 2:46















up vote
1
down vote

favorite












I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.



I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.



I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help










share|improve this question


















  • 2




    But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
    – jpp
    Nov 11 at 2:46













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.



I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.



I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help










share|improve this question













I have a string of integers, such as string = {11, 2, 14, 1, 17, 1, 123, 1} What I need to do is turn it into a list, and then sort them into two separate lists of odd and even elements.



I had a dictionary that looked like {11: 2, 14: 1, 17: 1, 123: 1}, converted it to a string, and removed the colons into commas, so now I have the first string. I did this so I can split up the elements into having the numbers 11, 14, 17, and 123 in one list, and 2, 1, 1, and 1 in the other list.



I think I can convert this string into a list, and then use a loop to append each element into the list I need it to be. The problem is, I can't figure out how to turn this string into a list again. How would I do that? Alternatively, can I split the values the way I want straight from the dictionary and save steps? Thanks for any help







python list dictionary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 2:39









Joel Banks

395




395








  • 2




    But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
    – jpp
    Nov 11 at 2:46














  • 2




    But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
    – jpp
    Nov 11 at 2:46








2




2




But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
– jpp
Nov 11 at 2:46




But {11, 2, 14, 1, 17, 1, 123, 1} isn't a string of integers.
– jpp
Nov 11 at 2:46












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.



>>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
>>> list(obj.keys())
[123, 17, 11, 14]
>>> list(obj.values())
[1, 1, 2, 1]





share|improve this answer





















  • Perfect, thank you
    – Joel Banks
    Nov 11 at 2:48


















up vote
0
down vote













String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:



from collections import defaultdict

x = {11: 2, 14: 1, 17: 1, 123: 1}

dd = defaultdict(list)

for key in x:
dd['odd' if key % 2 else 'even'].append(key)


The result is a dictionary mapping of odd and even keys:



defaultdict(list, {'odd': [11, 17, 123],
'even': [14]})


You can then access odd keys via dd['odd'], even keys via dd['even'].






share|improve this answer




























    up vote
    0
    down vote













    To split a string into ints, use the built-in split method to return a list of each of the items:



    new_list = string.split(", ")





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245383%2fturning-a-string-of-integers-into-a-list-and-sorting-it-by-odd-and-even-elements%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.



      >>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
      >>> list(obj.keys())
      [123, 17, 11, 14]
      >>> list(obj.values())
      [1, 1, 2, 1]





      share|improve this answer





















      • Perfect, thank you
        – Joel Banks
        Nov 11 at 2:48















      up vote
      3
      down vote



      accepted










      Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.



      >>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
      >>> list(obj.keys())
      [123, 17, 11, 14]
      >>> list(obj.values())
      [1, 1, 2, 1]





      share|improve this answer





















      • Perfect, thank you
        – Joel Banks
        Nov 11 at 2:48













      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.



      >>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
      >>> list(obj.keys())
      [123, 17, 11, 14]
      >>> list(obj.values())
      [1, 1, 2, 1]





      share|improve this answer












      Yes, you can split the values straight from the dictionary! You'd use the .keys() and .values() on the dictionary object.



      >>> obj = {11: 2, 14: 1, 17: 1, 123: 1}
      >>> list(obj.keys())
      [123, 17, 11, 14]
      >>> list(obj.values())
      [1, 1, 2, 1]






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 11 at 2:42









      UltraInstinct

      30.3k76090




      30.3k76090












      • Perfect, thank you
        – Joel Banks
        Nov 11 at 2:48


















      • Perfect, thank you
        – Joel Banks
        Nov 11 at 2:48
















      Perfect, thank you
      – Joel Banks
      Nov 11 at 2:48




      Perfect, thank you
      – Joel Banks
      Nov 11 at 2:48












      up vote
      0
      down vote













      String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:



      from collections import defaultdict

      x = {11: 2, 14: 1, 17: 1, 123: 1}

      dd = defaultdict(list)

      for key in x:
      dd['odd' if key % 2 else 'even'].append(key)


      The result is a dictionary mapping of odd and even keys:



      defaultdict(list, {'odd': [11, 17, 123],
      'even': [14]})


      You can then access odd keys via dd['odd'], even keys via dd['even'].






      share|improve this answer

























        up vote
        0
        down vote













        String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:



        from collections import defaultdict

        x = {11: 2, 14: 1, 17: 1, 123: 1}

        dd = defaultdict(list)

        for key in x:
        dd['odd' if key % 2 else 'even'].append(key)


        The result is a dictionary mapping of odd and even keys:



        defaultdict(list, {'odd': [11, 17, 123],
        'even': [14]})


        You can then access odd keys via dd['odd'], even keys via dd['even'].






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:



          from collections import defaultdict

          x = {11: 2, 14: 1, 17: 1, 123: 1}

          dd = defaultdict(list)

          for key in x:
          dd['odd' if key % 2 else 'even'].append(key)


          The result is a dictionary mapping of odd and even keys:



          defaultdict(list, {'odd': [11, 17, 123],
          'even': [14]})


          You can then access odd keys via dd['odd'], even keys via dd['even'].






          share|improve this answer












          String conversion isn't necessary. In fact, there's no need to create a list of all keys for your problem. You can use collections.defaultdict and iterate your dictionary:



          from collections import defaultdict

          x = {11: 2, 14: 1, 17: 1, 123: 1}

          dd = defaultdict(list)

          for key in x:
          dd['odd' if key % 2 else 'even'].append(key)


          The result is a dictionary mapping of odd and even keys:



          defaultdict(list, {'odd': [11, 17, 123],
          'even': [14]})


          You can then access odd keys via dd['odd'], even keys via dd['even'].







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 2:51









          jpp

          83.6k194896




          83.6k194896






















              up vote
              0
              down vote













              To split a string into ints, use the built-in split method to return a list of each of the items:



              new_list = string.split(", ")





              share|improve this answer

























                up vote
                0
                down vote













                To split a string into ints, use the built-in split method to return a list of each of the items:



                new_list = string.split(", ")





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  To split a string into ints, use the built-in split method to return a list of each of the items:



                  new_list = string.split(", ")





                  share|improve this answer












                  To split a string into ints, use the built-in split method to return a list of each of the items:



                  new_list = string.split(", ")






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 2:58









                  john doe

                  23




                  23






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245383%2fturning-a-string-of-integers-into-a-list-and-sorting-it-by-odd-and-even-elements%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly