Split an array in python using indexes from a list









up vote
-2
down vote

favorite












I have a 2d array of size 3 by 7 in numpy:



[[1 2 3 4 5 6 7]
[4 5 6 7 8 9 0]
[2 3 4 5 6 7 8]]


I also have a list that contains indexes of splitting points:



[1, 3]


Now, I want to split the array using the indexes in the list such that I get:



[[1 2]
[4 5]
[2 3]]

[[ 2 3 4]
[5 6 7]
[3 4 5]]

[[ 4 5 6 7]
[7 8 9 0]
[5 6 7 8]]


How can I do this in python?










share|improve this question



























    up vote
    -2
    down vote

    favorite












    I have a 2d array of size 3 by 7 in numpy:



    [[1 2 3 4 5 6 7]
    [4 5 6 7 8 9 0]
    [2 3 4 5 6 7 8]]


    I also have a list that contains indexes of splitting points:



    [1, 3]


    Now, I want to split the array using the indexes in the list such that I get:



    [[1 2]
    [4 5]
    [2 3]]

    [[ 2 3 4]
    [5 6 7]
    [3 4 5]]

    [[ 4 5 6 7]
    [7 8 9 0]
    [5 6 7 8]]


    How can I do this in python?










    share|improve this question

























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      I have a 2d array of size 3 by 7 in numpy:



      [[1 2 3 4 5 6 7]
      [4 5 6 7 8 9 0]
      [2 3 4 5 6 7 8]]


      I also have a list that contains indexes of splitting points:



      [1, 3]


      Now, I want to split the array using the indexes in the list such that I get:



      [[1 2]
      [4 5]
      [2 3]]

      [[ 2 3 4]
      [5 6 7]
      [3 4 5]]

      [[ 4 5 6 7]
      [7 8 9 0]
      [5 6 7 8]]


      How can I do this in python?










      share|improve this question















      I have a 2d array of size 3 by 7 in numpy:



      [[1 2 3 4 5 6 7]
      [4 5 6 7 8 9 0]
      [2 3 4 5 6 7 8]]


      I also have a list that contains indexes of splitting points:



      [1, 3]


      Now, I want to split the array using the indexes in the list such that I get:



      [[1 2]
      [4 5]
      [2 3]]

      [[ 2 3 4]
      [5 6 7]
      [3 4 5]]

      [[ 4 5 6 7]
      [7 8 9 0]
      [5 6 7 8]]


      How can I do this in python?







      python python-3.x list numpy indexing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      jpp

      79.8k184695




      79.8k184695










      asked 2 days ago









      mrbright

      224




      224






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can use a list comprehension with slicing, using zip to extract indices pairwise.



          A = np.array([[1, 2, 3, 4, 5, 6, 7],
          [4, 5, 6, 7, 8, 9, 0],
          [2, 3, 4, 5, 6, 7, 8]])

          idx = [1, 3]
          idx = [0] + idx + [A.shape[1]]

          res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])]

          print(*res, sep='n'*2)

          [[1 2]
          [4 5]
          [2 3]]

          [[2 3 4]
          [5 6 7]
          [3 4 5]]

          [[4 5 6 7]
          [7 8 9 0]
          [5 6 7 8]]





          share|improve this answer




















          • Thank you @jpp, It worked
            – mrbright
            2 days ago










          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%2f53225184%2fsplit-an-array-in-python-using-indexes-from-a-list%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can use a list comprehension with slicing, using zip to extract indices pairwise.



          A = np.array([[1, 2, 3, 4, 5, 6, 7],
          [4, 5, 6, 7, 8, 9, 0],
          [2, 3, 4, 5, 6, 7, 8]])

          idx = [1, 3]
          idx = [0] + idx + [A.shape[1]]

          res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])]

          print(*res, sep='n'*2)

          [[1 2]
          [4 5]
          [2 3]]

          [[2 3 4]
          [5 6 7]
          [3 4 5]]

          [[4 5 6 7]
          [7 8 9 0]
          [5 6 7 8]]





          share|improve this answer




















          • Thank you @jpp, It worked
            – mrbright
            2 days ago














          up vote
          2
          down vote



          accepted










          You can use a list comprehension with slicing, using zip to extract indices pairwise.



          A = np.array([[1, 2, 3, 4, 5, 6, 7],
          [4, 5, 6, 7, 8, 9, 0],
          [2, 3, 4, 5, 6, 7, 8]])

          idx = [1, 3]
          idx = [0] + idx + [A.shape[1]]

          res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])]

          print(*res, sep='n'*2)

          [[1 2]
          [4 5]
          [2 3]]

          [[2 3 4]
          [5 6 7]
          [3 4 5]]

          [[4 5 6 7]
          [7 8 9 0]
          [5 6 7 8]]





          share|improve this answer




















          • Thank you @jpp, It worked
            – mrbright
            2 days ago












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can use a list comprehension with slicing, using zip to extract indices pairwise.



          A = np.array([[1, 2, 3, 4, 5, 6, 7],
          [4, 5, 6, 7, 8, 9, 0],
          [2, 3, 4, 5, 6, 7, 8]])

          idx = [1, 3]
          idx = [0] + idx + [A.shape[1]]

          res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])]

          print(*res, sep='n'*2)

          [[1 2]
          [4 5]
          [2 3]]

          [[2 3 4]
          [5 6 7]
          [3 4 5]]

          [[4 5 6 7]
          [7 8 9 0]
          [5 6 7 8]]





          share|improve this answer












          You can use a list comprehension with slicing, using zip to extract indices pairwise.



          A = np.array([[1, 2, 3, 4, 5, 6, 7],
          [4, 5, 6, 7, 8, 9, 0],
          [2, 3, 4, 5, 6, 7, 8]])

          idx = [1, 3]
          idx = [0] + idx + [A.shape[1]]

          res = [A[:, start: end+1] for start, end in zip(idx, idx[1:])]

          print(*res, sep='n'*2)

          [[1 2]
          [4 5]
          [2 3]]

          [[2 3 4]
          [5 6 7]
          [3 4 5]]

          [[4 5 6 7]
          [7 8 9 0]
          [5 6 7 8]]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          jpp

          79.8k184695




          79.8k184695











          • Thank you @jpp, It worked
            – mrbright
            2 days ago
















          • Thank you @jpp, It worked
            – mrbright
            2 days ago















          Thank you @jpp, It worked
          – mrbright
          2 days ago




          Thank you @jpp, It worked
          – mrbright
          2 days ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225184%2fsplit-an-array-in-python-using-indexes-from-a-list%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo