Merged
Conversation
bbayles
approved these changes
Aug 1, 2025
Collaborator
|
Interesting! I thought this would use the ideas in your classic blog post. This is a very elegant alternative. |
Contributor
Author
|
The running median in the blog post needed an indexable skiplist (or sorted collections or a blist) because it was the median over a window instead of all values seen so far. Windowing requires the ability to delete old elements as well as add new ones, and the heap version can't do that (it is add only). If you're open to going beyond what is in the CPython docs, it would be nice to add an option for a running median over a sliding window: def running_median(iterable, *, window=None):
"""Cumulative median of values seen so far or values in a sliding window.
For example:
>>> list(running_median([5.0, 9.0, 4.0, 12.0, 8.0, 9.0]))
[5.0, 7.0, 5.0, 7.0, 8.0, 8.5]
>>> list(running_median([5.0, 9.0, 4.0, 12.0, 8.0, 9.0], window=3))
[5.0, 9.0, 8.0, 7.0, 8.0, 9.0]
Supports numeric types such as int, float, Decimal, and Fraction,
but not complex numbers which are unorderable.
""" |
Collaborator
|
Yeah, let's do the windowed version - would be nice to be able to do a very long running median. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a slightly improved version of
running_median()which was recently added to the CPython docs.