Oh... I made a mistake in my off-the-cuff code. The items.append()
shouldn't be in an else, but just in the loop.
def median(it, on_nan=DEFAULT):
if on_nan == 'unsafe':
... do all the current stuff ...
elif on_nan == "ignore":
return median((x for x in it if not is_nan(x)), on_nan='unsafe')
elif on_nan = "ieee_total_order":
return median(sorted(it, key=total_order), on_nan='unsafe')
else:
items = []
for x in it:
if is_nan(x):
if on_nan == 'raise':
raise ValueError('No median exists of collections with NaNs')
elif on_nan == 'poison':
return float('nan')
items.append(x)
return median(items, on_nan='unsafe')
It needs a total_order() support function and an is_nan() support function,
but other than that, I think this is complete code. Probably it would be
faster if it moved the raise/poison conditions outside the loop, and looped
inside each of them. But this way is slightly fewer lines and conceptually
more obvious, I think.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/LUDK5X55SVV5NIEC5W7BGSLSUZKBY7U5/
Code of Conduct: http://python.org/psf/codeofconduct/