Changing Array of elements to multiple array of elements












-4















How do I split seperated elements in an array to new elements with separated elements within them.



The idea is to change this



[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]


to this



[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]


I tried this code but it didnt work. I set my_string to the top array.



my_list = my_string.split(",")









share|improve this question




















  • 5





    Would love to see what you've tried, and why it didn't work?

    – coldspeed
    Jan 2 at 6:22











  • please see above

    – john samcock
    Jan 2 at 6:26











  • what's the criteria to separate these?

    – AkshayNevrekar
    Jan 2 at 6:26











  • Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

    – john samcock
    Jan 2 at 6:27








  • 1





    @johnsamcock Check my answer. I hope it helps.

    – CodeIt
    Jan 2 at 6:30
















-4















How do I split seperated elements in an array to new elements with separated elements within them.



The idea is to change this



[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]


to this



[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]


I tried this code but it didnt work. I set my_string to the top array.



my_list = my_string.split(",")









share|improve this question




















  • 5





    Would love to see what you've tried, and why it didn't work?

    – coldspeed
    Jan 2 at 6:22











  • please see above

    – john samcock
    Jan 2 at 6:26











  • what's the criteria to separate these?

    – AkshayNevrekar
    Jan 2 at 6:26











  • Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

    – john samcock
    Jan 2 at 6:27








  • 1





    @johnsamcock Check my answer. I hope it helps.

    – CodeIt
    Jan 2 at 6:30














-4












-4








-4








How do I split seperated elements in an array to new elements with separated elements within them.



The idea is to change this



[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]


to this



[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]


I tried this code but it didnt work. I set my_string to the top array.



my_list = my_string.split(",")









share|improve this question
















How do I split seperated elements in an array to new elements with separated elements within them.



The idea is to change this



[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]


to this



[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]


I tried this code but it didnt work. I set my_string to the top array.



my_list = my_string.split(",")






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 6:25







john samcock

















asked Jan 2 at 6:21









john samcockjohn samcock

267




267








  • 5





    Would love to see what you've tried, and why it didn't work?

    – coldspeed
    Jan 2 at 6:22











  • please see above

    – john samcock
    Jan 2 at 6:26











  • what's the criteria to separate these?

    – AkshayNevrekar
    Jan 2 at 6:26











  • Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

    – john samcock
    Jan 2 at 6:27








  • 1





    @johnsamcock Check my answer. I hope it helps.

    – CodeIt
    Jan 2 at 6:30














  • 5





    Would love to see what you've tried, and why it didn't work?

    – coldspeed
    Jan 2 at 6:22











  • please see above

    – john samcock
    Jan 2 at 6:26











  • what's the criteria to separate these?

    – AkshayNevrekar
    Jan 2 at 6:26











  • Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

    – john samcock
    Jan 2 at 6:27








  • 1





    @johnsamcock Check my answer. I hope it helps.

    – CodeIt
    Jan 2 at 6:30








5




5





Would love to see what you've tried, and why it didn't work?

– coldspeed
Jan 2 at 6:22





Would love to see what you've tried, and why it didn't work?

– coldspeed
Jan 2 at 6:22













please see above

– john samcock
Jan 2 at 6:26





please see above

– john samcock
Jan 2 at 6:26













what's the criteria to separate these?

– AkshayNevrekar
Jan 2 at 6:26





what's the criteria to separate these?

– AkshayNevrekar
Jan 2 at 6:26













Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

– john samcock
Jan 2 at 6:27







Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.

– john samcock
Jan 2 at 6:27






1




1





@johnsamcock Check my answer. I hope it helps.

– CodeIt
Jan 2 at 6:30





@johnsamcock Check my answer. I hope it helps.

– CodeIt
Jan 2 at 6:30












3 Answers
3






active

oldest

votes


















4














Try this:



# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array


You can also use the below one liner list comprehension method.



mystring = [string.split(",") for string in mystring]


Output:



[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


See the code in action here.






share|improve this answer

































    4














    [i.split(',') for i in list_of_words]


    output:



    [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


    I think it helps!






    share|improve this answer



















    • 1





      wow ok i understand now we are just splitting based on comma and we can use pythons code

      – john samcock
      Jan 2 at 6:35



















    2














    Use list comprehension with split()



    l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]

    print([i.split(",") for i in l])


    Output:



    [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
    ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]





    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',
      autoActivateHeartbeat: false,
      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%2f54002070%2fchanging-array-of-elements-to-multiple-array-of-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









      4














      Try this:



      # initial list
      mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
      # empty list to store new values
      array =
      # loop through the list and split each value
      for i in mystring:
      array.append(i.split(",")) # splits into list and appends it a new list
      print(array) # prints the resultant array


      You can also use the below one liner list comprehension method.



      mystring = [string.split(",") for string in mystring]


      Output:



      [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


      See the code in action here.






      share|improve this answer






























        4














        Try this:



        # initial list
        mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
        # empty list to store new values
        array =
        # loop through the list and split each value
        for i in mystring:
        array.append(i.split(",")) # splits into list and appends it a new list
        print(array) # prints the resultant array


        You can also use the below one liner list comprehension method.



        mystring = [string.split(",") for string in mystring]


        Output:



        [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


        See the code in action here.






        share|improve this answer




























          4












          4








          4







          Try this:



          # initial list
          mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
          # empty list to store new values
          array =
          # loop through the list and split each value
          for i in mystring:
          array.append(i.split(",")) # splits into list and appends it a new list
          print(array) # prints the resultant array


          You can also use the below one liner list comprehension method.



          mystring = [string.split(",") for string in mystring]


          Output:



          [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


          See the code in action here.






          share|improve this answer















          Try this:



          # initial list
          mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
          # empty list to store new values
          array =
          # loop through the list and split each value
          for i in mystring:
          array.append(i.split(",")) # splits into list and appends it a new list
          print(array) # prints the resultant array


          You can also use the below one liner list comprehension method.



          mystring = [string.split(",") for string in mystring]


          Output:



          [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


          See the code in action here.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 6:35

























          answered Jan 2 at 6:29









          CodeItCodeIt

          66011020




          66011020

























              4














              [i.split(',') for i in list_of_words]


              output:



              [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


              I think it helps!






              share|improve this answer



















              • 1





                wow ok i understand now we are just splitting based on comma and we can use pythons code

                – john samcock
                Jan 2 at 6:35
















              4














              [i.split(',') for i in list_of_words]


              output:



              [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


              I think it helps!






              share|improve this answer



















              • 1





                wow ok i understand now we are just splitting based on comma and we can use pythons code

                – john samcock
                Jan 2 at 6:35














              4












              4








              4







              [i.split(',') for i in list_of_words]


              output:



              [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


              I think it helps!






              share|improve this answer













              [i.split(',') for i in list_of_words]


              output:



              [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]


              I think it helps!







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 2 at 6:30









              CSMaverickCSMaverick

              1,5131628




              1,5131628








              • 1





                wow ok i understand now we are just splitting based on comma and we can use pythons code

                – john samcock
                Jan 2 at 6:35














              • 1





                wow ok i understand now we are just splitting based on comma and we can use pythons code

                – john samcock
                Jan 2 at 6:35








              1




              1





              wow ok i understand now we are just splitting based on comma and we can use pythons code

              – john samcock
              Jan 2 at 6:35





              wow ok i understand now we are just splitting based on comma and we can use pythons code

              – john samcock
              Jan 2 at 6:35











              2














              Use list comprehension with split()



              l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]

              print([i.split(",") for i in l])


              Output:



              [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
              ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]





              share|improve this answer




























                2














                Use list comprehension with split()



                l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]

                print([i.split(",") for i in l])


                Output:



                [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
                ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]





                share|improve this answer


























                  2












                  2








                  2







                  Use list comprehension with split()



                  l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]

                  print([i.split(",") for i in l])


                  Output:



                  [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
                  ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]





                  share|improve this answer













                  Use list comprehension with split()



                  l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]

                  print([i.split(",") for i in l])


                  Output:



                  [['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
                  ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 6:31









                  AkshayNevrekarAkshayNevrekar

                  4,87191840




                  4,87191840






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002070%2fchanging-array-of-elements-to-multiple-array-of-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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas