Finding items satisfying a condition in a list using patterns

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












Why the following command



Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y]


doesn't return the y values that are greater than their neighbors in the list?
I am expecting to have an output like 6,8.










share|improve this question









New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
    – The Legend of 1991
    Sep 30 at 23:46














up vote
7
down vote

favorite












Why the following command



Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y]


doesn't return the y values that are greater than their neighbors in the list?
I am expecting to have an output like 6,8.










share|improve this question









New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
    – The Legend of 1991
    Sep 30 at 23:46












up vote
7
down vote

favorite









up vote
7
down vote

favorite











Why the following command



Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y]


doesn't return the y values that are greater than their neighbors in the list?
I am expecting to have an output like 6,8.










share|improve this question









New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Why the following command



Cases[1, 2, 6, 3, 4, 8, 5, x_, y_, z_ /; (y > Max[x, z]):>y]


doesn't return the y values that are greater than their neighbors in the list?
I am expecting to have an output like 6,8.







pattern-matching filtering






share|improve this question









New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Sep 30 at 23:47









Henrik Schumacher

41k258124




41k258124






New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Sep 30 at 23:45









The Legend of 1991

603




603




New contributor




The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






The Legend of 1991 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
    – The Legend of 1991
    Sep 30 at 23:46
















  • For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
    – The Legend of 1991
    Sep 30 at 23:46















For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
– The Legend of 1991
Sep 30 at 23:46




For this question, I want to know how can I do it using patterns and I am not looking for alternative solutions.
– The Legend of 1991
Sep 30 at 23:46










3 Answers
3






active

oldest

votes

















up vote
7
down vote



accepted










There are no Lists of length 3 in the input at level 1. So Cases cannot find anything. However SequenceCases can because it interprets lists as sequences.



SequenceCases[
1, 2, 6, 3, 4, 8, 5,
x_, y_, z_ /; (y > Max[x, z]) :> y,
Overlaps -> True
]



6, 8







share|improve this answer




















  • But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
    – The Legend of 1991
    Oct 1 at 6:29










  • Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
    – Henrik Schumacher
    Oct 1 at 6:33


















up vote
3
down vote













ReplaceList with a slight modification of the pattern in OP gives the desired result:



ReplaceList[1, 2, 6, 3, 4, 8, 5, ___, x_, y_, z_, ___ /; (y > Max[x, z]) :> y]



6, 8







share|improve this answer





























    up vote
    2
    down vote













    The approach proposed in the question works well with a slight modification.



    Cases[Partition[1, 2, 6, 3, 4, 8, 5, 3, 1], x_, y_, z_ /; (y > Max[x, z]) :> y]
    (* 6, 8 *)


    There are many other approaches as well, for instance,



    list = 1, 2, 6, 3, 4, 8, 5;
    MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
    list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]]
    (* 6, 8 *)


    Timing



    Runtime for the MapThread solution with a list of length of 40000 is, for instance,



    list = RandomInteger[0, 9, 40000];
    AbsoluteTiming[MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
    list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]][[1 ;; 10]]]


    about 0.06 seconds on my computer and increases approximately linearly with the length of list. The solution using Cases and Partition requires about the same amount of time. Surprisingly, the solution with ReplaceList is about two orders of magnitude slower and increases approximately quadratically with the length of list. The solution using SequenceCases is many orders of magnitude slower and increases at least cubically with the length of list.






    share|improve this answer






















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      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: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182901%2ffinding-items-satisfying-a-condition-in-a-list-using-patterns%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote



      accepted










      There are no Lists of length 3 in the input at level 1. So Cases cannot find anything. However SequenceCases can because it interprets lists as sequences.



      SequenceCases[
      1, 2, 6, 3, 4, 8, 5,
      x_, y_, z_ /; (y > Max[x, z]) :> y,
      Overlaps -> True
      ]



      6, 8







      share|improve this answer




















      • But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
        – The Legend of 1991
        Oct 1 at 6:29










      • Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
        – Henrik Schumacher
        Oct 1 at 6:33















      up vote
      7
      down vote



      accepted










      There are no Lists of length 3 in the input at level 1. So Cases cannot find anything. However SequenceCases can because it interprets lists as sequences.



      SequenceCases[
      1, 2, 6, 3, 4, 8, 5,
      x_, y_, z_ /; (y > Max[x, z]) :> y,
      Overlaps -> True
      ]



      6, 8







      share|improve this answer




















      • But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
        – The Legend of 1991
        Oct 1 at 6:29










      • Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
        – Henrik Schumacher
        Oct 1 at 6:33













      up vote
      7
      down vote



      accepted







      up vote
      7
      down vote



      accepted






      There are no Lists of length 3 in the input at level 1. So Cases cannot find anything. However SequenceCases can because it interprets lists as sequences.



      SequenceCases[
      1, 2, 6, 3, 4, 8, 5,
      x_, y_, z_ /; (y > Max[x, z]) :> y,
      Overlaps -> True
      ]



      6, 8







      share|improve this answer












      There are no Lists of length 3 in the input at level 1. So Cases cannot find anything. However SequenceCases can because it interprets lists as sequences.



      SequenceCases[
      1, 2, 6, 3, 4, 8, 5,
      x_, y_, z_ /; (y > Max[x, z]) :> y,
      Overlaps -> True
      ]



      6, 8








      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 30 at 23:56









      Henrik Schumacher

      41k258124




      41k258124











      • But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
        – The Legend of 1991
        Oct 1 at 6:29










      • Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
        – Henrik Schumacher
        Oct 1 at 6:33

















      • But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
        – The Legend of 1991
        Oct 1 at 6:29










      • Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
        – Henrik Schumacher
        Oct 1 at 6:33
















      But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
      – The Legend of 1991
      Oct 1 at 6:29




      But why this one Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] also doesn't work? I have a list of length 3 in level 1.
      – The Legend of 1991
      Oct 1 at 6:29












      Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
      – Henrik Schumacher
      Oct 1 at 6:33





      Because the list is still not on level one. But Cases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y] does return 6. Reading this might give you the whole picture.
      – Henrik Schumacher
      Oct 1 at 6:33











      up vote
      3
      down vote













      ReplaceList with a slight modification of the pattern in OP gives the desired result:



      ReplaceList[1, 2, 6, 3, 4, 8, 5, ___, x_, y_, z_, ___ /; (y > Max[x, z]) :> y]



      6, 8







      share|improve this answer


























        up vote
        3
        down vote













        ReplaceList with a slight modification of the pattern in OP gives the desired result:



        ReplaceList[1, 2, 6, 3, 4, 8, 5, ___, x_, y_, z_, ___ /; (y > Max[x, z]) :> y]



        6, 8







        share|improve this answer
























          up vote
          3
          down vote










          up vote
          3
          down vote









          ReplaceList with a slight modification of the pattern in OP gives the desired result:



          ReplaceList[1, 2, 6, 3, 4, 8, 5, ___, x_, y_, z_, ___ /; (y > Max[x, z]) :> y]



          6, 8







          share|improve this answer














          ReplaceList with a slight modification of the pattern in OP gives the desired result:



          ReplaceList[1, 2, 6, 3, 4, 8, 5, ___, x_, y_, z_, ___ /; (y > Max[x, z]) :> y]



          6, 8








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 1 at 4:09

























          answered Oct 1 at 3:03









          kglr

          164k8188388




          164k8188388




















              up vote
              2
              down vote













              The approach proposed in the question works well with a slight modification.



              Cases[Partition[1, 2, 6, 3, 4, 8, 5, 3, 1], x_, y_, z_ /; (y > Max[x, z]) :> y]
              (* 6, 8 *)


              There are many other approaches as well, for instance,



              list = 1, 2, 6, 3, 4, 8, 5;
              MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
              list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]]
              (* 6, 8 *)


              Timing



              Runtime for the MapThread solution with a list of length of 40000 is, for instance,



              list = RandomInteger[0, 9, 40000];
              AbsoluteTiming[MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
              list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]][[1 ;; 10]]]


              about 0.06 seconds on my computer and increases approximately linearly with the length of list. The solution using Cases and Partition requires about the same amount of time. Surprisingly, the solution with ReplaceList is about two orders of magnitude slower and increases approximately quadratically with the length of list. The solution using SequenceCases is many orders of magnitude slower and increases at least cubically with the length of list.






              share|improve this answer


























                up vote
                2
                down vote













                The approach proposed in the question works well with a slight modification.



                Cases[Partition[1, 2, 6, 3, 4, 8, 5, 3, 1], x_, y_, z_ /; (y > Max[x, z]) :> y]
                (* 6, 8 *)


                There are many other approaches as well, for instance,



                list = 1, 2, 6, 3, 4, 8, 5;
                MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]]
                (* 6, 8 *)


                Timing



                Runtime for the MapThread solution with a list of length of 40000 is, for instance,



                list = RandomInteger[0, 9, 40000];
                AbsoluteTiming[MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]][[1 ;; 10]]]


                about 0.06 seconds on my computer and increases approximately linearly with the length of list. The solution using Cases and Partition requires about the same amount of time. Surprisingly, the solution with ReplaceList is about two orders of magnitude slower and increases approximately quadratically with the length of list. The solution using SequenceCases is many orders of magnitude slower and increases at least cubically with the length of list.






                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The approach proposed in the question works well with a slight modification.



                  Cases[Partition[1, 2, 6, 3, 4, 8, 5, 3, 1], x_, y_, z_ /; (y > Max[x, z]) :> y]
                  (* 6, 8 *)


                  There are many other approaches as well, for instance,



                  list = 1, 2, 6, 3, 4, 8, 5;
                  MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                  list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]]
                  (* 6, 8 *)


                  Timing



                  Runtime for the MapThread solution with a list of length of 40000 is, for instance,



                  list = RandomInteger[0, 9, 40000];
                  AbsoluteTiming[MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                  list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]][[1 ;; 10]]]


                  about 0.06 seconds on my computer and increases approximately linearly with the length of list. The solution using Cases and Partition requires about the same amount of time. Surprisingly, the solution with ReplaceList is about two orders of magnitude slower and increases approximately quadratically with the length of list. The solution using SequenceCases is many orders of magnitude slower and increases at least cubically with the length of list.






                  share|improve this answer














                  The approach proposed in the question works well with a slight modification.



                  Cases[Partition[1, 2, 6, 3, 4, 8, 5, 3, 1], x_, y_, z_ /; (y > Max[x, z]) :> y]
                  (* 6, 8 *)


                  There are many other approaches as well, for instance,



                  list = 1, 2, 6, 3, 4, 8, 5;
                  MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                  list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]]
                  (* 6, 8 *)


                  Timing



                  Runtime for the MapThread solution with a list of length of 40000 is, for instance,



                  list = RandomInteger[0, 9, 40000];
                  AbsoluteTiming[MapThread[If[#1 > Max[#2, #3], #1, Nothing] &,
                  list[[2 ;; -2]], list[[1 ;; -3]], list[[3 ;; -1]]][[1 ;; 10]]]


                  about 0.06 seconds on my computer and increases approximately linearly with the length of list. The solution using Cases and Partition requires about the same amount of time. Surprisingly, the solution with ReplaceList is about two orders of magnitude slower and increases approximately quadratically with the length of list. The solution using SequenceCases is many orders of magnitude slower and increases at least cubically with the length of list.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 1 at 11:11

























                  answered Oct 1 at 10:24









                  bbgodfrey

                  42.9k857104




                  42.9k857104




















                      The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.












                      The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.











                      The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182901%2ffinding-items-satisfying-a-condition-in-a-list-using-patterns%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?