I would like to take a list of numbers in Python and replace all zeros with a previous non-zero value, for example. [1, 0, 0, 2, 0]must give [1,1,1,2,2]. I know that the first entry in the list is nonzero, so initialization should not be a problem. I can do this in a loop, but is there a more pythonic way to do this?
To clarify: this should work with any number list, for example. [100.7, 0, -2.34]must become [100.7, 100.7, -2.34].
source
share