Dropping lists using pattern [duplicate]











up vote
3
down vote

favorite













This question already has an answer here:




  • Matching a case when one element in a pair is Null

    2 answers




I have the following list of lists.



 data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}


I want to drop all the lists with 0 as the 3rd entry i.e. {m,n,0}. How can I do this?










share|improve this question













marked as duplicate by Jason B., Henrik Schumacher, Kuba list-manipulation
Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 3 at 21:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    3
    down vote

    favorite













    This question already has an answer here:




    • Matching a case when one element in a pair is Null

      2 answers




    I have the following list of lists.



     data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}


    I want to drop all the lists with 0 as the 3rd entry i.e. {m,n,0}. How can I do this?










    share|improve this question













    marked as duplicate by Jason B., Henrik Schumacher, Kuba list-manipulation
    Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 3 at 21:46


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite












      This question already has an answer here:




      • Matching a case when one element in a pair is Null

        2 answers




      I have the following list of lists.



       data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}


      I want to drop all the lists with 0 as the 3rd entry i.e. {m,n,0}. How can I do this?










      share|improve this question














      This question already has an answer here:




      • Matching a case when one element in a pair is Null

        2 answers




      I have the following list of lists.



       data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}


      I want to drop all the lists with 0 as the 3rd entry i.e. {m,n,0}. How can I do this?





      This question already has an answer here:




      • Matching a case when one element in a pair is Null

        2 answers








      list-manipulation pattern-matching






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 3 at 16:53









      Physics Moron

      253111




      253111




      marked as duplicate by Jason B., Henrik Schumacher, Kuba list-manipulation
      Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 3 at 21:46


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jason B., Henrik Schumacher, Kuba list-manipulation
      Users with the  list-manipulation badge can single-handedly close list-manipulation questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 3 at 21:46


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Easy (but probably not the fastest) way:



          DeleteCases[data, {_, _, 0}]



          {{2, 0, 1}, {5, 0, 3}}




          This might be a bit faster for longer lists as it entirely avoids pattern matching:



          Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]


          Here a speed test:



          data = RandomInteger[{0, 5}, {1000000, 3}];
          a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
          b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
          AbsoluteTiming // First
          a == b



          0.407339



          0.027375



          True







          share|improve this answer






























            up vote
            1
            down vote













            Another way



            data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
            Pick[data, Unitize@data[[;; , 3]], 1]



            {{2, 0, 1}, {5, 0, 3}}




            And a timing comparison to DeleteCases



            SeedRandom[1234]
            Block[
            {data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
            times = {
            AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
            AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
            };
            {times, m1 == m2}
            ]



            {{0.403632, 0.031339}, True}







            share|improve this answer




























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              Easy (but probably not the fastest) way:



              DeleteCases[data, {_, _, 0}]



              {{2, 0, 1}, {5, 0, 3}}




              This might be a bit faster for longer lists as it entirely avoids pattern matching:



              Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]


              Here a speed test:



              data = RandomInteger[{0, 5}, {1000000, 3}];
              a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
              b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
              AbsoluteTiming // First
              a == b



              0.407339



              0.027375



              True







              share|improve this answer



























                up vote
                4
                down vote



                accepted










                Easy (but probably not the fastest) way:



                DeleteCases[data, {_, _, 0}]



                {{2, 0, 1}, {5, 0, 3}}




                This might be a bit faster for longer lists as it entirely avoids pattern matching:



                Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]


                Here a speed test:



                data = RandomInteger[{0, 5}, {1000000, 3}];
                a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
                b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
                AbsoluteTiming // First
                a == b



                0.407339



                0.027375



                True







                share|improve this answer

























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  Easy (but probably not the fastest) way:



                  DeleteCases[data, {_, _, 0}]



                  {{2, 0, 1}, {5, 0, 3}}




                  This might be a bit faster for longer lists as it entirely avoids pattern matching:



                  Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]


                  Here a speed test:



                  data = RandomInteger[{0, 5}, {1000000, 3}];
                  a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
                  b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
                  AbsoluteTiming // First
                  a == b



                  0.407339



                  0.027375



                  True







                  share|improve this answer














                  Easy (but probably not the fastest) way:



                  DeleteCases[data, {_, _, 0}]



                  {{2, 0, 1}, {5, 0, 3}}




                  This might be a bit faster for longer lists as it entirely avoids pattern matching:



                  Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]


                  Here a speed test:



                  data = RandomInteger[{0, 5}, {1000000, 3}];
                  a = DeleteCases[data, {_, _, 0}]; // AbsoluteTiming // First
                  b = Delete[data, Partition[Random`Private`PositionsOf[data[[All, 3]], 0], 1]]; //
                  AbsoluteTiming // First
                  a == b



                  0.407339



                  0.027375



                  True








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 3 at 18:30

























                  answered Dec 3 at 16:58









                  Henrik Schumacher

                  47.4k466134




                  47.4k466134






















                      up vote
                      1
                      down vote













                      Another way



                      data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
                      Pick[data, Unitize@data[[;; , 3]], 1]



                      {{2, 0, 1}, {5, 0, 3}}




                      And a timing comparison to DeleteCases



                      SeedRandom[1234]
                      Block[
                      {data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
                      times = {
                      AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
                      AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
                      };
                      {times, m1 == m2}
                      ]



                      {{0.403632, 0.031339}, True}







                      share|improve this answer

























                        up vote
                        1
                        down vote













                        Another way



                        data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
                        Pick[data, Unitize@data[[;; , 3]], 1]



                        {{2, 0, 1}, {5, 0, 3}}




                        And a timing comparison to DeleteCases



                        SeedRandom[1234]
                        Block[
                        {data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
                        times = {
                        AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
                        AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
                        };
                        {times, m1 == m2}
                        ]



                        {{0.403632, 0.031339}, True}







                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          Another way



                          data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
                          Pick[data, Unitize@data[[;; , 3]], 1]



                          {{2, 0, 1}, {5, 0, 3}}




                          And a timing comparison to DeleteCases



                          SeedRandom[1234]
                          Block[
                          {data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
                          times = {
                          AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
                          AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
                          };
                          {times, m1 == m2}
                          ]



                          {{0.403632, 0.031339}, True}







                          share|improve this answer












                          Another way



                          data = {{0, 0, 0}, {1, 0, 0}, {2, 0, 1}, {3, 0, 0}, {4, 0, 0}, {5, 0, 3}}
                          Pick[data, Unitize@data[[;; , 3]], 1]



                          {{2, 0, 1}, {5, 0, 3}}




                          And a timing comparison to DeleteCases



                          SeedRandom[1234]
                          Block[
                          {data = RandomInteger[{0, 5}, {1000000, 3}], times, m1, m2},
                          times = {
                          AbsoluteTiming[m1 = DeleteCases[data, {__, 0}]][[1]],
                          AbsoluteTiming[m2 = Pick[#, Unitize@#[[;; , 3]], 1] &@data][[1]]
                          };
                          {times, m1 == m2}
                          ]



                          {{0.403632, 0.031339}, True}








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 3 at 18:41









                          That Gravity Guy

                          2,1011515




                          2,1011515















                              Popular posts from this blog

                              flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                              Mangá

                               ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕