I have a list: {1,2,3,0,0,4,5,0,0,0}
. I want to delete the 0
's occurring after the last positive integer. In other words, I want Mathematica to return: {1,2,3,0,0,4,5}
.
Answer
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.
Comments
Post a Comment