Mathematica: Idiomatic way to replace values in a list that match a condition?

The built in function Chop is almost exactly what you're looking for (it does work on lists, as in your example). One potential surprise is that it doesn't chop (truncate) integers, only floating point numbers. So for your example to work as you might expect, first convert your list to floating point with the N function: ChopN@{-3, -2, -1, 0, 1, 2, 3}, 1.5 -> {-3.

, -2. , 0, 0, 0, 2. , 3.

} As Ramashalanka shows, to do this sort of thing more generally, I recommend: IfAbs.

Lots of options that all work: MapIfAbs# 0 which is equivalent to: {-3, -2, -1, 0, 1, 2, 3} /. (x_ /; Absx 0 or ReplaceAll{-3, -2, -1, 0, 1, 2, 3}, (x_?(Abs# 0 which is equivalent to: {-3, -2, -1, 0, 1, 2, 3} /. (x_?(Abs# 0.

Here is a method (f3) that is nearly an order of magnitude faster. It is adapted from code by Fred Simons. F1 = IfAbs# 0 &; f3 = # (1 - Unitize@Clip#, {-1.5, 1.5}, {0, 0}) &; lst = RandomInteger{-100, 100}, 5000; SameQ @@ (#@lst & /@ {f1, f2, f3}) First@Timing@Do#@lst, {500} & /@ {f1, f2, f3} (* Out= True *) (* Out= {0.406, 2.282, 0.047} *).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions