6
$\begingroup$

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?

$\endgroup$
4
  • 2
    $\begingroup$ try Cases[{a_,_}/;>=0]@yourlist or Select[Positive@*First]@yourlist $\endgroup$
    – kglr
    May 22 at 7:36
  • $\begingroup$ @kglr I receive this error: Syntax::: "{a_,_}/;" cannot be followed by ">=0". $\endgroup$
    – caren
    May 22 at 7:42
  • $\begingroup$ @kglr Thank's. this Select[Positive@*First]@yourlist is true. $\endgroup$
    – caren
    May 22 at 7:44
  • 1
    $\begingroup$ sorry, it should be Cases[{a_,_}/;a>=0]@yourlist $\endgroup$
    – kglr
    May 22 at 7:51

4 Answers 4

7
$\begingroup$

There 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]]

Mathematica graphics

You can also do

p = FirstPosition[lst, {0, _}]
lst[[Range[First@p, Length@lst]]]

Mathematica graphics

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

Mathematica graphics

But you really want

Mathematica graphics

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]], {}]

Mathematica graphics

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]], {}]

Mathematica graphics

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.

$\endgroup$
4
  • $\begingroup$ Thank you so much. $\endgroup$
    – caren
    May 22 at 7:46
  • $\begingroup$ Thanks.I don't care this point. And if I want new list with just before zero? $\endgroup$
    – caren
    May 22 at 8:05
  • 1
    $\begingroup$ @caren I would still use 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$
    – Nasser
    May 22 at 8:14
  • $\begingroup$ Be negative or positive value it's not important. I want to separate list from this position. $\endgroup$
    – caren
    May 23 at 10:36
9
$\begingroup$

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}}
$\endgroup$
5
$\begingroup$

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!

$\endgroup$
4
  • 2
    $\begingroup$ This does not really work. Same issue as with Cases., Try it on this 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$
    – Nasser
    May 22 at 17:59
  • $\begingroup$ @Nasser Thanks, mate! $\endgroup$ May 22 at 19:35
  • 1
    $\begingroup$ nice solution. Now it works. $\endgroup$
    – Nasser
    May 22 at 19:43
  • 1
    $\begingroup$ I'm so grateful. $\endgroup$
    – caren
    May 23 at 6:46
2
$\begingroup$

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}}

$\endgroup$
1
  • $\begingroup$ I want to separate the list from this position. $\endgroup$
    – caren
    May 23 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.