[issue30999] statistics module: add "key" keyword argument to median, mode, ...

Steven D'Aprano report at bugs.python.org
Sun Jul 23 20:43:59 EDT 2017


Steven D'Aprano added the comment:

Apart from being "cool", what is the purpose of this key argument?

For the example shown, where you extract an item from tuple data:

>>> median_low([(1, 2), (3, 3), (4, 1)], key=lambda elem: elem[0])
(3, 3)

I'm not sure I understand when you would use this, and why you would describe (3,3) as a median (a kind of average) of the given data.


By the way, although it's not (yet?) officially supported, it turns out that this works:

py> median_low([(1, 2), (3, 3), (4, 1)])
(3, 3)

Officially, median requires numeric data. If the median* functions were to support tuples, I would be inclined to return a new tuple with the median of each column, as such:

median_low([(1, 2), (3, 3), (4, 1)])
(3, 2)  # median of 1,3,4 and median of 2,3,1


I can think of uses for that, e.g. calculating the "Q" correlation coefficient. What uses do you have for your suggested key argument?

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30999>
_______________________________________


More information about the Python-bugs-list mailing list