How we can remove all pairs {x,y}
of a list which begin from a special x
.
For example:
list={{1,2},{4,5},{5,9},{8,13},{9,0.1},{15,-0.2},{17,-9},{23,2}};
We want keep pairs in which x=<9
:
extractlist={{1,2},{4,5},{5,9},{8,13},{9,0.1}};
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communitylist = {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}, {15, -0.2}, {17, -9}, {23,
2}};
trimmedList = Select[list, #[[1]] <= 9 &]
(* {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}} *)
Some other ways
trimmedList ==
Cases[list, _?(#[[1]] <= 9 &)] ==
Cases[list, {_?(# <= 9 &), _}] ==
DeleteCases[list, _?(#[[1]] > 9 &)] ==
DeleteCases[list, {_?(# > 9 &), _}]
(* True *)
I think:
extractlist=DeleteCases[list, {x_, y_} /; x > 9]
does the job.
Select[list, #[[1]] <= 9 &]
Select is a reasonably simple method.
If the xs are ordered in list
list[[;; Last[Ordering[Ordering[Append[list[[-1 ;; 1 ;; -1, 1]], 9.]]]] - 1]]
{{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}}
Otherwise
Pick[list, Clip[list[[All, 1]], {9., 9.}, {9., 0}], 9.]
{{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1}}
list = {{1, 2}, {4, 5}, {5, 9}, {8, 13}, {9, 0.1},
{15, -0.2}, {17, -9}, {23, 2}} /. {x_, _} /; x > 9 -> Sequence[]
TakeWhile[list, #[[1]] <= 9 &]
may be (yet another) option worth considering. $\endgroup$