I have a list like this:
{{-0.0019,1},{-0.0018,2},...,{0,11},{0.001,6},...}
I need to make a new list that start from zero value. like this:
{{0,11},{0.001,6},...}
How can I do?
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 communityThere are probably 10 ways to do this.
lst = {{-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
lst[[ First@FirstPosition[lst, {0, _}] ;; -1]]
You can also do
p = FirstPosition[lst, {0, _}]
lst[[Range[First@p, Length@lst]]]
Update
Actually Cases
is not what you want. Since you want to start your new list only after the {0,_}
entry in the old list.
a new list that start from zero value
For example. given
lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001,6}, {0.002, 6}}
Cases[lst, {x_, _} /; x >= 0]
Gives
But you really want
Same issue with Select[Positive@*First]@yourlist
.
So I think using FirstPosition
is what you want to use.
And if I want new list with just before zero?
lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
p = First@FirstPosition[lst, {0, _}]
If[p > 1, lst[[1;;p-1]], {}]
lst = {{0,20},{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}}
p = First@FirstPosition[lst, {0, _}]
If[p > 1, lst[[1;;p-1]], {}]
ps. This assumes at least one {0,_}
entry exists in the list. Otherwise, I would add a check for NotFound
being returned from FirstPosition
as well to make code more robust.
FirstPosition
but now count from 1 up to one less that that value. Updated. Just need special case, for when {0,_}
shows on first position.
$\endgroup$
Drop
+ LengthWhile
Drop[#, LengthWhile[#, First @ # != 0 &]] & @ lst
{{0, 11}, {0.001, 6}, {0.002, 6}}
NestWhile
+ Rest
NestWhile[Rest, lst, #[[1, 1]] != 0 &]
{{0, 11}, {0.001, 6}, {0.002, 6}}
ReplaceAll
ReplaceAll[{___, a : {0, _}, b___} :> {a, b}]@lst
{{0, 11}, {0.001, 6}, {0.002, 6}}
Another way, using FirstCase
and Position
:
lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};
lst[[Det@Position[lst, FirstCase[lst, {0, _}]] ;;]]
(*{{0, 11}, {0.001, 6}, {0.002, 6}}*)
Or using FirstPosition
and TakeList
:
Last@TakeList[lst, {FirstPosition[lst, {0, _}][[1]] - 1, UpTo[Length[lst]]}]
(* (*{{0, 11}, {0.001, 6}, {0.002, 6}}*)*)
Thanks for your excellent observation, Nasser!
lst = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};
it should return {{0, 11}, {0.001, 6}, {0.002, 6}}
but your solution returns {{1, 10}, {0, 11}, {0.001, 6}, {0.002, 6}}
.
$\endgroup$
Let n
be the index of the original list of pairs pairList
such that newList = pairList[[n;;]]
is desired and for each element elem
in newList
, First@elem
is non-negative. Then the desired list is yielded by
pairList = {{1, 10}, {-0.0019, 1}, {-0.0018, 2}, {0, 11}, {0.001, 6}, {0.002, 6}};
SplitBy[pairList, NonNegative@*First] // Last
{{0, 11}, {0.001, 6}, {0.002, 6}}
Cases[{a_,_}/;>=0]@yourlist
orSelect[Positive@*First]@yourlist
$\endgroup$Cases[{a_,_}/;a>=0]@yourlist
$\endgroup$