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
(data)
| 325 | |
| 326 | |
| 327 | def median(data): |
| 328 | """Return the median (middle value) of numeric data. |
| 329 | |
| 330 | When the number of data points is odd, return the middle data point. |
| 331 | When the number of data points is even, the median is interpolated by |
| 332 | taking the average of the two middle values: |
| 333 | |
| 334 | >>> median([1, 3, 5]) |
| 335 | 3 |
| 336 | >>> median([1, 3, 5, 7]) |
| 337 | 4.0 |
| 338 | |
| 339 | """ |
| 340 | data = sorted(data) |
| 341 | n = len(data) |
| 342 | if n == 0: |
| 343 | raise StatisticsError("no median for empty data") |
| 344 | if n % 2 == 1: |
| 345 | return data[n // 2] |
| 346 | else: |
| 347 | i = n // 2 |
| 348 | return (data[i - 1] + data[i]) / 2 |
| 349 | |
| 350 | |
| 351 | def median_low(data): |
searching dependent graphs…