[Python-ideas] Module name [was Re: Pre-PEP: adding a statistics module to Python]
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Aug 6 15:09:58 CEST 2013
On 6 August 2013 08:49, Mark Dickinson <dickinsm at gmail.com> wrote:
> The complex numbers argument seems like a red herring to me: I can't
> imagine why we'd ever want a cmath.statistics module. I can't see any
> problem with having future functions in math.statistics that can either take
> float inputs and return a float result, or take complex inputs and return a
> complex result.
The complex numbers argument is a red herring. Steven's module already
accepts whatever types make sense in the appropriate context,
including complex numbers:
>>> from statistics import *
>>> mean([1j, 2j, 3j])
2j
>>> mode([1, 2, 3j, 3j])
3j
>>> median([1j, 2j])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".\statistics.py", line 462, in __new__
data = sorted(data)
TypeError: unorderable types: complex() < complex()
>>> from fractions import Fraction
>>> dicerolls = list(range(1, 6+1))
>>> dicerolls
[1, 2, 3, 4, 5, 6]
>>> mean(dicerolls)
3.5
>>> fdice = [Fraction(n) for n in dicerolls]
>>> fdice
[Fraction(1, 1), Fraction(2, 1), Fraction(3, 1), Fraction(4, 1),
Fraction(5, 1), Fraction(6, 1)]
>>> mean(fdice)
Fraction(7, 2)
>>> print(mean(fdice))
7/2
>>> print(pvariance(fdice))
35/12
>>> mode('abracadabra')
'a'
>>> median.low(['spam', 'ham', 'eggs'])
'ham'
>>> from datetime import datetime, timedelta
>>> now = datetime.now()
>>> dates = [now + timedelta(days=n) for n in range(5)]
>>> dates
[datetime.datetime(2013, 8, 6, 14, 5, 35, 614027),
datetime.datetime(2013, 8, 7, 14, 5, 35, 614027),
datetime.datetime(2013, 8, 8, 14, 5, 35, 614027),
datetime.datetime(2013, 8, 9, 14, 5, 35, 614027),
datetime.datetime(2013, 8, 10, 14,
5, 35, 614027)]
>>> median.low(dates)
datetime.datetime(2013, 8, 8, 14, 5, 35, 614027)
Oscar
More information about the Python-ideas
mailing list