[Python-ideas] Proposal: A Reduce-Map Comprehension and a "last" builtin
Peter O'Connor
peter.ed.oconnor at gmail.com
Thu Apr 5 12:52:17 EDT 2018
Dear all,
In Python, I often find myself building lists where each element depends on
the last. This generally means making a for-loop, create an initial list,
and appending to it in the loop, or creating a generator-function. Both of
these feel more verbose than necessary.
I was thinking it would be nice to be able to encapsulate this common type
of operation into a more compact comprehension.
I propose a new "Reduce-Map" comprehension that allows us to write:
signal = [math.sin(i*0.01) + random.normalvariate(0, 0.1) for i in range(1000)]
smooth_signal = [average = (1-decay)*average + decay*x for x in signal
from average=0.]
Instead of:
def exponential_moving_average(signal: Iterable[float], decay: float,
initial_value: float=0.):
average = initial_value
for xt in signal:
average = (1-decay)*average + decay*xt
yield average
signal = [math.sin(i*0.01) + random.normalvariate(0, 0.1) for i in range(1000)]
smooth_signal = list(exponential_moving_average(signal, decay=0.05))
I've created a complete proposal at:
https://github.com/petered/peps/blob/master/pep-9999.rst , (and a
pull-request <https://github.com/python/peps/pull/609>) and I'd be
interested to hear what people think of this idea.
Combined with the new "last" builtin discussed in the proposal, this would
allow u to replace "reduce" with a more Pythonic comprehension-style
syntax.
- Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180405/bf16259e/attachment.html>
More information about the Python-ideas
mailing list