Make all sublists same length by prepending nan












0














How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?



import random
[list(range(0,random.randint(1,5))) for x in range(n)]


So if the output is:




[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]




It should look like:




[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]











share|improve this question


















  • 1




    What have you tried so far?
    – Idlehands
    Nov 20 '18 at 19:18










  • @Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
    – jchaykow
    Nov 20 '18 at 19:22










  • You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
    – Idlehands
    Nov 20 '18 at 19:25






  • 1




    Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
    – Bram Vanroy
    Nov 20 '18 at 19:25










  • @BramVanroy it's a joke, I wrote two comments
    – jchaykow
    Nov 20 '18 at 19:26
















0














How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?



import random
[list(range(0,random.randint(1,5))) for x in range(n)]


So if the output is:




[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]




It should look like:




[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]











share|improve this question


















  • 1




    What have you tried so far?
    – Idlehands
    Nov 20 '18 at 19:18










  • @Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
    – jchaykow
    Nov 20 '18 at 19:22










  • You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
    – Idlehands
    Nov 20 '18 at 19:25






  • 1




    Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
    – Bram Vanroy
    Nov 20 '18 at 19:25










  • @BramVanroy it's a joke, I wrote two comments
    – jchaykow
    Nov 20 '18 at 19:26














0












0








0







How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?



import random
[list(range(0,random.randint(1,5))) for x in range(n)]


So if the output is:




[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]




It should look like:




[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]











share|improve this question













How can I make all of my sublists the same length (the length of the longest sublist) by prepending np.nan's on each sublist?



import random
[list(range(0,random.randint(1,5))) for x in range(n)]


So if the output is:




[[0, 1], [0], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3]]




It should look like:




[[nan,nan,nan,0, 1], [nan,nan,nan,nan,0], [0, 1, 2, 3, 4], [0, 1, 2,
3, 4], [nan,0, 1, 2, 3]]








python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 19:16









jchaykow

497318




497318








  • 1




    What have you tried so far?
    – Idlehands
    Nov 20 '18 at 19:18










  • @Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
    – jchaykow
    Nov 20 '18 at 19:22










  • You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
    – Idlehands
    Nov 20 '18 at 19:25






  • 1




    Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
    – Bram Vanroy
    Nov 20 '18 at 19:25










  • @BramVanroy it's a joke, I wrote two comments
    – jchaykow
    Nov 20 '18 at 19:26














  • 1




    What have you tried so far?
    – Idlehands
    Nov 20 '18 at 19:18










  • @Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
    – jchaykow
    Nov 20 '18 at 19:22










  • You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
    – Idlehands
    Nov 20 '18 at 19:25






  • 1




    Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
    – Bram Vanroy
    Nov 20 '18 at 19:25










  • @BramVanroy it's a joke, I wrote two comments
    – jchaykow
    Nov 20 '18 at 19:26








1




1




What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18




What have you tried so far?
– Idlehands
Nov 20 '18 at 19:18












@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22




@Idlehands I've tried reversing first and then .extend() and then reverse again. But this doesn't work if the list is empty
– jchaykow
Nov 20 '18 at 19:22












You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
– Idlehands
Nov 20 '18 at 19:25




You might be overthinking it... how is the list of sublists supposed to be generated? Is it actual data or is it created with the nan paddings from the beginning?
– Idlehands
Nov 20 '18 at 19:25




1




1




Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25




Saying 'everything' is not useful. Show us the actual code that you tried, which gives us either a starting point or a hint what we should not try.
– Bram Vanroy
Nov 20 '18 at 19:25












@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26




@BramVanroy it's a joke, I wrote two comments
– jchaykow
Nov 20 '18 at 19:26












4 Answers
4






active

oldest

votes


















3














First, find the length of the longest sublist using max(). Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaNs followed by the original list.



import random, math

n = 5
lists = [list(range(0,random.randint(1,5))) for x in range(n)]

# get the maximum length
maxlen = len(max(lists, key=len))

# pad left of each sublist with NaN to make it as long as the longest
for sublist in lists:
sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist





share|improve this answer





















  • Accepting this one because it's just barely faster than @Torin May
    – jchaykow
    Nov 20 '18 at 19:38










  • It'll get a lot faster as your n goes up.
    – kindall
    Nov 20 '18 at 19:50



















3














import random
import numpy as np

n = 5
a = [list(range(0,random.randint(1,5))) for x in range(n)]
for c in a:
while len(c) < n:
c.insert(0, np.nan)
print(a)





[[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]






This is an example of what I think you are looking for.






share|improve this answer





























    0














    Not the most pythonic code but I think it does the trick:



    longest_list_length = 0
    for sublist in output:
    if len(sublist) > longest_list_length:
    longest_list_length = len(sublist)

    for sublist in output:
    nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
    sublist = nans_to_prepend + sublist





    share|improve this answer





















    • replacing the value of sublist won't change the value in the original list.
      – kindall
      Nov 20 '18 at 19:28



















    0














    If all you care about is generating the np.nan padding during the random creation, this simple one liner will suffice:



    [[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

    # [[nan, 0, 1, 2, 3],
    # [nan, nan, 0, 1, 2],
    # [nan, nan, nan, 0, 1],
    # [nan, 0, 1, 2, 3],
    # [0, 1, 2, 3, 4]]


    You can of course replace 5 with n and determine your max length.






    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%2f53400016%2fmake-all-sublists-same-length-by-prepending-nan%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      First, find the length of the longest sublist using max(). Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaNs followed by the original list.



      import random, math

      n = 5
      lists = [list(range(0,random.randint(1,5))) for x in range(n)]

      # get the maximum length
      maxlen = len(max(lists, key=len))

      # pad left of each sublist with NaN to make it as long as the longest
      for sublist in lists:
      sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist





      share|improve this answer





















      • Accepting this one because it's just barely faster than @Torin May
        – jchaykow
        Nov 20 '18 at 19:38










      • It'll get a lot faster as your n goes up.
        – kindall
        Nov 20 '18 at 19:50
















      3














      First, find the length of the longest sublist using max(). Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaNs followed by the original list.



      import random, math

      n = 5
      lists = [list(range(0,random.randint(1,5))) for x in range(n)]

      # get the maximum length
      maxlen = len(max(lists, key=len))

      # pad left of each sublist with NaN to make it as long as the longest
      for sublist in lists:
      sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist





      share|improve this answer





















      • Accepting this one because it's just barely faster than @Torin May
        – jchaykow
        Nov 20 '18 at 19:38










      • It'll get a lot faster as your n goes up.
        – kindall
        Nov 20 '18 at 19:50














      3












      3








      3






      First, find the length of the longest sublist using max(). Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaNs followed by the original list.



      import random, math

      n = 5
      lists = [list(range(0,random.randint(1,5))) for x in range(n)]

      # get the maximum length
      maxlen = len(max(lists, key=len))

      # pad left of each sublist with NaN to make it as long as the longest
      for sublist in lists:
      sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist





      share|improve this answer












      First, find the length of the longest sublist using max(). Then, for each sublist, use a slice assignment to replace the content of that list with the right number of NaNs followed by the original list.



      import random, math

      n = 5
      lists = [list(range(0,random.randint(1,5))) for x in range(n)]

      # get the maximum length
      maxlen = len(max(lists, key=len))

      # pad left of each sublist with NaN to make it as long as the longest
      for sublist in lists:
      sublist[:] = [math.nan] * (maxlen - len(sublist)) + sublist






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 '18 at 19:27









      kindall

      127k17192241




      127k17192241












      • Accepting this one because it's just barely faster than @Torin May
        – jchaykow
        Nov 20 '18 at 19:38










      • It'll get a lot faster as your n goes up.
        – kindall
        Nov 20 '18 at 19:50


















      • Accepting this one because it's just barely faster than @Torin May
        – jchaykow
        Nov 20 '18 at 19:38










      • It'll get a lot faster as your n goes up.
        – kindall
        Nov 20 '18 at 19:50
















      Accepting this one because it's just barely faster than @Torin May
      – jchaykow
      Nov 20 '18 at 19:38




      Accepting this one because it's just barely faster than @Torin May
      – jchaykow
      Nov 20 '18 at 19:38












      It'll get a lot faster as your n goes up.
      – kindall
      Nov 20 '18 at 19:50




      It'll get a lot faster as your n goes up.
      – kindall
      Nov 20 '18 at 19:50













      3














      import random
      import numpy as np

      n = 5
      a = [list(range(0,random.randint(1,5))) for x in range(n)]
      for c in a:
      while len(c) < n:
      c.insert(0, np.nan)
      print(a)





      [[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]






      This is an example of what I think you are looking for.






      share|improve this answer


























        3














        import random
        import numpy as np

        n = 5
        a = [list(range(0,random.randint(1,5))) for x in range(n)]
        for c in a:
        while len(c) < n:
        c.insert(0, np.nan)
        print(a)





        [[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]






        This is an example of what I think you are looking for.






        share|improve this answer
























          3












          3








          3






          import random
          import numpy as np

          n = 5
          a = [list(range(0,random.randint(1,5))) for x in range(n)]
          for c in a:
          while len(c) < n:
          c.insert(0, np.nan)
          print(a)





          [[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]






          This is an example of what I think you are looking for.






          share|improve this answer












          import random
          import numpy as np

          n = 5
          a = [list(range(0,random.randint(1,5))) for x in range(n)]
          for c in a:
          while len(c) < n:
          c.insert(0, np.nan)
          print(a)





          [[nan, nan, nan, nan, 0], [nan, nan, nan, nan, 0], [nan, nan, nan, 0, 1], [nan, nan, 0, 1, 2], [nan, nan, nan, 0, 1]]






          This is an example of what I think you are looking for.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 19:24









          Torin May

          303214




          303214























              0














              Not the most pythonic code but I think it does the trick:



              longest_list_length = 0
              for sublist in output:
              if len(sublist) > longest_list_length:
              longest_list_length = len(sublist)

              for sublist in output:
              nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
              sublist = nans_to_prepend + sublist





              share|improve this answer





















              • replacing the value of sublist won't change the value in the original list.
                – kindall
                Nov 20 '18 at 19:28
















              0














              Not the most pythonic code but I think it does the trick:



              longest_list_length = 0
              for sublist in output:
              if len(sublist) > longest_list_length:
              longest_list_length = len(sublist)

              for sublist in output:
              nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
              sublist = nans_to_prepend + sublist





              share|improve this answer





















              • replacing the value of sublist won't change the value in the original list.
                – kindall
                Nov 20 '18 at 19:28














              0












              0








              0






              Not the most pythonic code but I think it does the trick:



              longest_list_length = 0
              for sublist in output:
              if len(sublist) > longest_list_length:
              longest_list_length = len(sublist)

              for sublist in output:
              nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
              sublist = nans_to_prepend + sublist





              share|improve this answer












              Not the most pythonic code but I think it does the trick:



              longest_list_length = 0
              for sublist in output:
              if len(sublist) > longest_list_length:
              longest_list_length = len(sublist)

              for sublist in output:
              nans_to_prepend = ['nan'] * (longest_list_length - len(sublist))
              sublist = nans_to_prepend + sublist






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 20 '18 at 19:26









              ritratt

              74031328




              74031328












              • replacing the value of sublist won't change the value in the original list.
                – kindall
                Nov 20 '18 at 19:28


















              • replacing the value of sublist won't change the value in the original list.
                – kindall
                Nov 20 '18 at 19:28
















              replacing the value of sublist won't change the value in the original list.
              – kindall
              Nov 20 '18 at 19:28




              replacing the value of sublist won't change the value in the original list.
              – kindall
              Nov 20 '18 at 19:28











              0














              If all you care about is generating the np.nan padding during the random creation, this simple one liner will suffice:



              [[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

              # [[nan, 0, 1, 2, 3],
              # [nan, nan, 0, 1, 2],
              # [nan, nan, nan, 0, 1],
              # [nan, 0, 1, 2, 3],
              # [0, 1, 2, 3, 4]]


              You can of course replace 5 with n and determine your max length.






              share|improve this answer


























                0














                If all you care about is generating the np.nan padding during the random creation, this simple one liner will suffice:



                [[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

                # [[nan, 0, 1, 2, 3],
                # [nan, nan, 0, 1, 2],
                # [nan, nan, nan, 0, 1],
                # [nan, 0, 1, 2, 3],
                # [0, 1, 2, 3, 4]]


                You can of course replace 5 with n and determine your max length.






                share|improve this answer
























                  0












                  0








                  0






                  If all you care about is generating the np.nan padding during the random creation, this simple one liner will suffice:



                  [[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

                  # [[nan, 0, 1, 2, 3],
                  # [nan, nan, 0, 1, 2],
                  # [nan, nan, nan, 0, 1],
                  # [nan, 0, 1, 2, 3],
                  # [0, 1, 2, 3, 4]]


                  You can of course replace 5 with n and determine your max length.






                  share|improve this answer












                  If all you care about is generating the np.nan padding during the random creation, this simple one liner will suffice:



                  [[np.nan]*(5-j) + list(range(j)) for j in (random.randint(1, 5) for x in range(5))]

                  # [[nan, 0, 1, 2, 3],
                  # [nan, nan, 0, 1, 2],
                  # [nan, nan, nan, 0, 1],
                  # [nan, 0, 1, 2, 3],
                  # [0, 1, 2, 3, 4]]


                  You can of course replace 5 with n and determine your max length.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 19:36









                  Idlehands

                  3,9871417




                  3,9871417






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53400016%2fmake-all-sublists-same-length-by-prepending-nan%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin