Slice list of lists at regular interval then merge










0















How would you "columnize" a list of lists at every xth index?



I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x.



For example with an interval of 2:



start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]


Just wondering if there's a quick way of doing this?










share|improve this question
























  • Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

    – Black Owl Kai
    Nov 13 '18 at 16:14











  • This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

    – gustavovelascoh
    Nov 13 '18 at 16:30
















0















How would you "columnize" a list of lists at every xth index?



I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x.



For example with an interval of 2:



start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]


Just wondering if there's a quick way of doing this?










share|improve this question
























  • Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

    – Black Owl Kai
    Nov 13 '18 at 16:14











  • This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

    – gustavovelascoh
    Nov 13 '18 at 16:30














0












0








0








How would you "columnize" a list of lists at every xth index?



I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x.



For example with an interval of 2:



start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]


Just wondering if there's a quick way of doing this?










share|improve this question
















How would you "columnize" a list of lists at every xth index?



I was thinking of doing this by taking the starting list, then creating two new lists, merging them, then adding the remainder not divisible by x.



For example with an interval of 2:



start = [
[1, 'one'],
[2, 'two'],
[3, 'three'],
[4, 'four'],
[5, 'five'],
[6, 'six'],
[7, 'seven'],
[8, 'eight'],
[9, 'nine'],
]
expected = [
[1, 'one', 3, 'three'],
[2, 'two', 4, 'four'],
# page break
[5, 'five', 7, 'seven'],
[6, 'six', 8, 'eight'],
# page break
[9, 'nine'],
]


Just wondering if there's a quick way of doing this?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 16:17







bdoubleu

















asked Nov 13 '18 at 16:06









bdoubleubdoubleu

617112




617112












  • Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

    – Black Owl Kai
    Nov 13 '18 at 16:14











  • This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

    – gustavovelascoh
    Nov 13 '18 at 16:30


















  • Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

    – Black Owl Kai
    Nov 13 '18 at 16:14











  • This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

    – gustavovelascoh
    Nov 13 '18 at 16:30

















Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

– Black Owl Kai
Nov 13 '18 at 16:14





Could you check your testcase, and if it is right this way, explain how you get it? I would think of 1-3-5-7-9 - 2-4-6-8 and not 1-3 - 2-4 - 5-7 - 6-8 - 9 as a "columnisation" of a list

– Black Owl Kai
Nov 13 '18 at 16:14













This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

– gustavovelascoh
Nov 13 '18 at 16:30






This makes more sense for 'columnise': C=2; [[y for x in start[C*z:C*z+C] for y in x] for z in range(len(start)//C+1)]

– gustavovelascoh
Nov 13 '18 at 16:30













2 Answers
2






active

oldest

votes


















0














I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:



def columnize(A, interval=2):
ans =
for i in range(0,len(A), interval*2):
for j in range(min(interval, len(A)-i)):
ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
return ans





share|improve this answer






























    0














    Are you looking for something like this? Columnizing squared matrix?



    start = [
    [1, 'one'],
    [2, 'two'],
    [3, 'three'],
    [4, 'four'],
    [5, 'five'],
    [6, 'six'],
    [7, 'seven'],
    [8, 'eight'],
    [9, 'nine'],
    ]
    expected = [
    [1, 'one', 3, 'three'],
    [2, 'two', 4, 'four'],
    # page break
    [5, 'five', 7, 'seven'],
    [6, 'six', 8, 'eight'],
    # page break
    [9, 'nine'],
    ]
    a = 2
    r = a*a
    ans =
    for i in range(0, len(start), r):
    l_tmp = start[i:i+r]
    if l_tmp[::a]:
    ans.append([item for sublist in l_tmp[::a] for item in sublist])
    if l_tmp[1::a]:
    ans.append([item for sublist in l_tmp[1::a] for item in sublist])
    # You can easily add page break here
    print(ans)





    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%2f53284995%2fslice-list-of-lists-at-regular-interval-then-merge%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:



      def columnize(A, interval=2):
      ans =
      for i in range(0,len(A), interval*2):
      for j in range(min(interval, len(A)-i)):
      ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
      return ans





      share|improve this answer



























        0














        I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:



        def columnize(A, interval=2):
        ans =
        for i in range(0,len(A), interval*2):
        for j in range(min(interval, len(A)-i)):
        ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
        return ans





        share|improve this answer

























          0












          0








          0







          I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:



          def columnize(A, interval=2):
          ans =
          for i in range(0,len(A), interval*2):
          for j in range(min(interval, len(A)-i)):
          ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
          return ans





          share|improve this answer













          I agree with the comments about this being a strange way to 'collumize'. However, here is a function that does what you described:



          def columnize(A, interval=2):
          ans =
          for i in range(0,len(A), interval*2):
          for j in range(min(interval, len(A)-i)):
          ans.append(A[i+j] + (A[i+j+interval] if i+j+interval < len(A) else ))
          return ans






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 16:37









          wowserxwowserx

          42018




          42018























              0














              Are you looking for something like this? Columnizing squared matrix?



              start = [
              [1, 'one'],
              [2, 'two'],
              [3, 'three'],
              [4, 'four'],
              [5, 'five'],
              [6, 'six'],
              [7, 'seven'],
              [8, 'eight'],
              [9, 'nine'],
              ]
              expected = [
              [1, 'one', 3, 'three'],
              [2, 'two', 4, 'four'],
              # page break
              [5, 'five', 7, 'seven'],
              [6, 'six', 8, 'eight'],
              # page break
              [9, 'nine'],
              ]
              a = 2
              r = a*a
              ans =
              for i in range(0, len(start), r):
              l_tmp = start[i:i+r]
              if l_tmp[::a]:
              ans.append([item for sublist in l_tmp[::a] for item in sublist])
              if l_tmp[1::a]:
              ans.append([item for sublist in l_tmp[1::a] for item in sublist])
              # You can easily add page break here
              print(ans)





              share|improve this answer



























                0














                Are you looking for something like this? Columnizing squared matrix?



                start = [
                [1, 'one'],
                [2, 'two'],
                [3, 'three'],
                [4, 'four'],
                [5, 'five'],
                [6, 'six'],
                [7, 'seven'],
                [8, 'eight'],
                [9, 'nine'],
                ]
                expected = [
                [1, 'one', 3, 'three'],
                [2, 'two', 4, 'four'],
                # page break
                [5, 'five', 7, 'seven'],
                [6, 'six', 8, 'eight'],
                # page break
                [9, 'nine'],
                ]
                a = 2
                r = a*a
                ans =
                for i in range(0, len(start), r):
                l_tmp = start[i:i+r]
                if l_tmp[::a]:
                ans.append([item for sublist in l_tmp[::a] for item in sublist])
                if l_tmp[1::a]:
                ans.append([item for sublist in l_tmp[1::a] for item in sublist])
                # You can easily add page break here
                print(ans)





                share|improve this answer

























                  0












                  0








                  0







                  Are you looking for something like this? Columnizing squared matrix?



                  start = [
                  [1, 'one'],
                  [2, 'two'],
                  [3, 'three'],
                  [4, 'four'],
                  [5, 'five'],
                  [6, 'six'],
                  [7, 'seven'],
                  [8, 'eight'],
                  [9, 'nine'],
                  ]
                  expected = [
                  [1, 'one', 3, 'three'],
                  [2, 'two', 4, 'four'],
                  # page break
                  [5, 'five', 7, 'seven'],
                  [6, 'six', 8, 'eight'],
                  # page break
                  [9, 'nine'],
                  ]
                  a = 2
                  r = a*a
                  ans =
                  for i in range(0, len(start), r):
                  l_tmp = start[i:i+r]
                  if l_tmp[::a]:
                  ans.append([item for sublist in l_tmp[::a] for item in sublist])
                  if l_tmp[1::a]:
                  ans.append([item for sublist in l_tmp[1::a] for item in sublist])
                  # You can easily add page break here
                  print(ans)





                  share|improve this answer













                  Are you looking for something like this? Columnizing squared matrix?



                  start = [
                  [1, 'one'],
                  [2, 'two'],
                  [3, 'three'],
                  [4, 'four'],
                  [5, 'five'],
                  [6, 'six'],
                  [7, 'seven'],
                  [8, 'eight'],
                  [9, 'nine'],
                  ]
                  expected = [
                  [1, 'one', 3, 'three'],
                  [2, 'two', 4, 'four'],
                  # page break
                  [5, 'five', 7, 'seven'],
                  [6, 'six', 8, 'eight'],
                  # page break
                  [9, 'nine'],
                  ]
                  a = 2
                  r = a*a
                  ans =
                  for i in range(0, len(start), r):
                  l_tmp = start[i:i+r]
                  if l_tmp[::a]:
                  ans.append([item for sublist in l_tmp[::a] for item in sublist])
                  if l_tmp[1::a]:
                  ans.append([item for sublist in l_tmp[1::a] for item in sublist])
                  # You can easily add page break here
                  print(ans)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 16:37









                  BlueSheepTokenBlueSheepToken

                  1,169415




                  1,169415



























                      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%2f53284995%2fslice-list-of-lists-at-regular-interval-then-merge%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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto