Message333305
When len(data) is odd, median returns the average of the two middle values. This average is computed using
i = n//2
return (data[i - 1] + data[i])/2
This results in the following behaviour
>>> from fractions import Fraction
>>> from statistics import median
>>> F1 = Fraction(1, 1)
>>> median([1])
1
>>> median([1, 1]) # Example 1.
1.0
>>> median([F1])
Fraction(1, 1)
>>> median([F1, F1])
Fraction(1, 1)
>>> median([2, 2, 1, F1]) # Example 2.
Fraction(3, 2)
>>> median([2, 2, F1, 1]) # Example 3.
1.5
Perhaps, when len(data) is odd, it would be better to test the two middle values for equality. This would resolve Example 1. It would not help with Examples 2 and 3, which might not have a satisfactory solution.
See also issue 33084. |
|
Date |
User |
Action |
Args |
2019-01-09 11:46:14 | jfine2358 | set | recipients:
+ jfine2358 |
2019-01-09 11:46:12 | jfine2358 | set | messageid: <1547034372.58.0.0557058004142.issue35698@roundup.psfhosted.org> |
2019-01-09 11:46:12 | jfine2358 | link | issue35698 messages |
2019-01-09 11:46:12 | jfine2358 | create | |
|