Can Mathematica delete some of the 0's from a list?
The easiest way would be to use
{1, 2, 3, 0, 0, 4, 5, 0, 0, 0} /. {a___, 0 ...} :> {a}
/.
is synonymous with ReplaceAll
. It tries to replace values on the left hand side with the rules on the right hand side. In this case {a___, 0...}
is the pattern; a___
matches zero or more elements, and 0...
matches zero or more zeroes. :> {a}
takes the a
that corresponds to the matched expression and returns it.
The TakeWhile
and replacement based answers will be very slow with large lists and/or large lists with many trailing zeroes.
Something like
dropper=With[{s = Split[#]}, If[s[[-1, 1]] == 0, Join @@ (Most@s), #]] &;
will be ~2000X faster on a list of 50K length vs a replacement solution, advantage growing with size. There are other even faster methods for really huge flat numeric lists... e.g.:
dropper2 =
With[{s = SparseArray[#, Automatic, 0]["NonzeroPositions"]},
If[s === {}, {}, #[[;; s[[-1, 1]]]]]] &;
Using:
test = Join[RandomChoice[{1, 10} -> {1, 0}, 100000], ConstantArray[0, 100000]];
The SparseArray
is ~10X faster than dropper
, which is itself ~15X faster than the TakeWhile
, which is itself ~365X faster than the replace-based solution, making the SparseArray
~45,000X faster for this test...
Technically, @Bill's and @Pickett's clean answers do not quite delete the 0s after "the last positive integer." A teeny alteration fixes Bill's answer:
{1, 2, 3, 0, 0, 4, 4.2, 0, 0, 0} /. {a___, b_Integer /; b > 0, 0 ..} -> {a, b}