Mean, median, and mode
Josiah Carlson
jcarlson at uci.edu
Mon Dec 6 15:45:38 EST 2004
mfuhr at fuhr.org (Michael Fuhr) wrote:
>
> Josiah Carlson <jcarlson at uci.edu> writes:
>
>
> > "Robert Brewer" <fumanchu at amor.org> wrote:
> > >
> > > Josiah Carlson wrote:
> > > >
> > > > median = lambda x: x.sort() or x[len(x)//2]
> > >
> > > That...is a really sneaky use of null return values. I like. :)
> >
> > Thank you, I'm just using a paradigm (exploiting lambdas) that I picked
> > up while going through various functional programming modules.
>
> print median([1, 2, 3, 4, 5, 6])
> 4
>
> Shouldn't the median be 3.5?
The wikipedia entry (as mentioned by Peter Hanson in another post) is
incomplete. There are 3 valid medians for even-lengthed sequences:
sorted(seq)[len(seq)//2]
sorted(seq)[len(seq)//2-1]
sum(sorted(seq)[len(seq)//2-1:len(seq)//2+2])/2.0
All are correct, what is /desired/ is generally application specific. If
you want the statistical median, 3.5 is the answer. If you want a
computer science median (for quicksort, etc.), 3 or 4 is sufficient. I
chose the shortest implementation (using the author's style, without
sorted, because I forgot about the new builtin), which gives the greater
(or equal) of the two possible computer science median values.
- Josiah
More information about the Python-list
mailing list