4
$\begingroup$

This question continues form Can Mathematica delete some of the 0's from a list?

This time I want to delete zeros before the first positive integer.

I have a list: {0,0,0,1,2,3,0,0,4,5}.

I want Mathematica to return: {1,2,3,0,0,4,5}.

From ciao's answer

dropper2 = With[{s = SparseArray[#, Automatic, 0]["NonzeroPositions"]}, If[s === {}, {}, #[[;; s[[-1, 1]]]]]] &;

Internal`DeleteTrailingZeros@{1, 2, 3, 0, 0, 4, 5, 0, 0, 0}

res0 = Internal`DeleteTrailingZeros@test;

returns the wanted results (which was deleting zeros after the integers).

But how can I modify this code into deleting first zeros?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jan 18 '16 at 4:11
  • 3
    $\begingroup$ Reverse before & after will do it. Might be better ways, though. $\endgroup$ – Michael E2 Jan 18 '16 at 4:13
  • 1
    $\begingroup$ #[[First@FirstPosition[#, a_?Positive] ;;]] &@{0, 0, 0, 1, 2, 3, 0, 0, 4, 5}. Would this work in general? I think so, unless your list can have negative numbers. $\endgroup$ – march Jan 18 '16 at 5:02
  • 3
    $\begingroup$ Drop[list, FirstPosition[list, _?(# != 0 &)][[1]] - 1] $\endgroup$ – Hubble07 Jan 18 '16 at 5:09
  • 4
    $\begingroup$ Reverse[Internal`DeleteTrailingZeros@Reverse[list]] but now I see Michael E2 already suggested it... $\endgroup$ – bill s Jan 18 '16 at 5:13
6
$\begingroup$
list = {0, 0, 0, 1, 2, 3, 0, 0, 4, 5};

Using ReplaceRepeated:

list //. {0, a___} :> {a}

{1, 2, 3, 0, 0, 4, 5}

$\endgroup$
4
$\begingroup$

Two ways:

From my comment:

Reverse@Internal`DeleteTrailingZeros@
  Reverse@{0, 0, 0, 1, 2, 3, 0, 0, 4, 5}
(*  {1, 2, 3, 0, 0, 4, 5}  *)

Using System` level functions:

Drop[#, LengthWhile[#, MatchQ[0]]] &[{0, 0, 0, 1, 2, 3, 0, 0, 4, 5}]
(*  {1, 2, 3, 0, 0, 4, 5}  *)
$\endgroup$
  • 1
    $\begingroup$ It can be simplified a bit by using the operator form of MatchQ: Drop[#, LengthWhile[#, MatchQ[0]]] & $\endgroup$ – Gustavo Delfino Dec 8 '16 at 20:24
  • $\begingroup$ @GustavoDelfino Thanks! $\endgroup$ – Michael E2 Dec 8 '16 at 21:35
1
$\begingroup$
list = {0, 0, 0, 1, 2, 3, 0, 0, 4, 5}

NestWhile[Rest, list, First@# == 0 &]

{1, 2, 3, 0, 0, 4, 5}

Or

While[First@list == 0, list = Rest@list]; list

{1, 2, 3, 0, 0, 4, 5}

If the entries of list are all single-digit integers you can also use

IntegerDigits@FromDigits@{0, 0, 0, 1, 2, 3, 0, 0, 4, 5}

{1, 2, 3, 0, 0, 4, 5}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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