Finding items satisfying a condition in a list using patterns
Clash 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.
pattern-matching filtering
New contributor
add a comment |Â
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.
pattern-matching filtering
New contributor
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
add a comment |Â
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.
pattern-matching filtering
New contributor
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
pattern-matching filtering
New contributor
New contributor
edited Sep 30 at 23:47
Henrik Schumacher
41k258124
41k258124
New contributor
asked Sep 30 at 23:45
The Legend of 1991
603
603
New contributor
New contributor
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
add a comment |Â
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
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
7
down vote
accepted
There are no List
s 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
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. ButCases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y]
does return6
. Reading this might give you the whole picture.
â Henrik Schumacher
Oct 1 at 6:33
add a comment |Â
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
add a comment |Â
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
.
add a comment |Â
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 List
s 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
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. ButCases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y]
does return6
. Reading this might give you the whole picture.
â Henrik Schumacher
Oct 1 at 6:33
add a comment |Â
up vote
7
down vote
accepted
There are no List
s 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
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. ButCases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y]
does return6
. Reading this might give you the whole picture.
â Henrik Schumacher
Oct 1 at 6:33
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
There are no List
s 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
There are no List
s 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
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. ButCases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y]
does return6
. Reading this might give you the whole picture.
â Henrik Schumacher
Oct 1 at 6:33
add a comment |Â
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. ButCases[1, 6, 4, x_, y_, z_ /; (y > Max[x, z]) :> y]
does return6
. 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
edited Oct 1 at 4:09
answered Oct 1 at 3:03
kglr
164k8188388
164k8188388
add a comment |Â
add a comment |Â
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
.
add a comment |Â
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
.
add a comment |Â
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
.
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
.
edited Oct 1 at 11:11
answered Oct 1 at 10:24
bbgodfrey
42.9k857104
42.9k857104
add a comment |Â
add a comment |Â
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.
The Legend of 1991 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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