Mean, median, and mode - third time's the charm!!!

Frohnhofer, James james.frohnhofer at csfb.com
Mon Dec 6 16:16:08 EST 2004


> 
> median = lambda x: x.sort() or ((len(x) % 2) and (x[(len(x)>>1)])) or
> (sum(x[((len(x)>>1)-1):(len(x)>>1)+1])/2.0)
> 
> >>> median( [2,3,4,5,6,7])
> 4.5
> >>> median( [2,3,4,5,6])
> 4

How about

>>> median = lambda x: x.sort() or (x[(len(x)-1)/2] + x[len(x)/2])/2.0
>>> median([2,3,4,5,6,7])
4.5
>>> median( [2,3,4,5,6])
4.0
>>> 


==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================




More information about the Python-list mailing list