median() does not currently take a key function. This is not hard to see. It could, but as I've written, I don't think that's the best approach.
In [16]: statistics.median?? Signature: statistics.median(data) Source: def median(data): """Return the median (middle value) of numeric data. When the number of data points is odd, return the middle data point. When the number of data points is even, the median is interpolated by taking the average of the two middle values: >>> median([1, 3, 5]) 3 >>> median([1, 3, 5, 7]) 4.0 """ data = sorted(data) n = len(data) if n == 0: raise StatisticsError("no median for empty data") if n%2 == 1: return data[n//2] else: i = n//2 return (data[i - 1] + data[i])/2 On Sun, Dec 29, 2019 at 8:27 PM Richard Damon <rich...@damon-family.org> wrote: > On 12/29/19 8:13 PM, David Mertz wrote: > > On Sun, Dec 29, 2019 at 8:00 PM Richard Damon > > <rich...@damon-family.org <mailto:rich...@damon-family.org>> wrote: > > > > Which is EXACTLY the reason I say that if this is important enough to > > fix in median, it is important enough to fix in sorted. sorted gives > > exactly the same nonsense result, it is only a bit more obvious > > because > > it gives all the points. Is [3, nan, 1, 2, 4] a sorted list? > > > > > > As me and Uncle Timmy have pointed out, it IS FIXED in sorted(). You > > just need to call: > > > > sorted_stuff = sorted(stuff, key=nan_aware_transform) > > > > Guido's time machine to the rescue... you've had this for more than a > > decade now. > > > So we just need to make available a suitable key function, and because > this issue was aimed at confusion for inexperienced users, make it the > default sorting floats. > > Since I think median passes parameters to sorted, it says it is also > solved for median (if you are willing to use that meaning for NaNs) > > -- > Richard Damon > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/AUGSQRWKBL3HQTIJVKIBNB64O25A5RLF/ > Code of Conduct: http://python.org/psf/codeofconduct/ > -- 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 -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/54S5CWIRJ6OBOC2OGQPQUFUOENCWE2IO/ Code of Conduct: http://python.org/psf/codeofconduct/