[Python-checkins] python/nondist/sandbox/statistics statistics.py,
1.2, 1.3
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Sun Feb 15 02:22:36 EST 2004
Update of /cvsroot/python/python/nondist/sandbox/statistics
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29711
Modified Files:
statistics.py
Log Message:
Small cleanups.
Index: statistics.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/statistics/statistics.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** statistics.py 15 Feb 2004 06:45:09 -0000 1.2
--- statistics.py 15 Feb 2004 07:22:33 -0000 1.3
***************
*** 7,13 ****
and make only a single pass over the data. This makes the tools suitable
for use with generator expressions and keeps them as memory friendly as
! possible. Except for median(), the use of iterators means that the
! dataset can be processed one element at a time without bringing it all
! into memory.
The functions, nlargest and nsmallest, are also designed to use the fewest
--- 7,13 ----
and make only a single pass over the data. This makes the tools suitable
for use with generator expressions and keeps them as memory friendly as
! possible. Except for median() and select(), the use of iterators means
! that the dataset can be processed one element at a time without bringing
! it all into memory.
The functions, nlargest and nsmallest, are also designed to use the fewest
***************
*** 15,21 ****
as strings) as long they support __cmp__ or __lt__.
- The code is designed to run at its best on Py2.4; however,
- it being kept fully compatible with Py2.3.
-
Written and maintained by Raymond D. Hettinger <python at rcn.com>.
Copyright (c) 2004 Python Software Foundation. All rights reserved.
--- 15,18 ----
***************
*** 48,52 ****
raise ValueError('data must have at least one element')
-
def stddev(data, sample=True):
"""Computes the standard deviation of the dataset.
--- 45,48 ----
***************
*** 101,117 ****
ua, nua = under.append, notunder.append
it = iter(iterable)
! try:
! pivot = it.next()
! except StopIteration:
! pass
for elem in it:
if elem < pivot:
! under.append(elem)
else:
! notunder.append(elem)
return under, pivot, notunder
def select(data, n):
! """Find the nth rank ordered element"""
while 1:
p, pivot, q = _partition(data)
--- 97,112 ----
ua, nua = under.append, notunder.append
it = iter(iterable)
! pivot = it.next() # must have at least one element
for elem in it:
if elem < pivot:
! ua(elem)
else:
! nua(elem)
return under, pivot, notunder
def select(data, n):
! """Find the nth rank ordered element (the least value has rank 0)."""
! if len(data) <= n:
! raise ValueError('not enough elements for the given rank')
while 1:
p, pivot, q = _partition(data)
***************
*** 120,127 ****
elif n == len(p):
return pivot
! else :
data = q
n -= len(p) + 1
- p = q = None # Free unused partition
def median(data):
--- 115,121 ----
elif n == len(p):
return pivot
! else:
data = q
n -= len(p) + 1
def median(data):
***************
*** 163,168 ****
## When collections.fibheap is available, use it instead of bisect
it = iter(iterable)
! result = list(itertools.islice(it, 0, n))
! result.sort()
if len(result) == 0:
raise ValueError('data must have at least one element')
--- 157,161 ----
## When collections.fibheap is available, use it instead of bisect
it = iter(iterable)
! result = sorted(itertools.islice(it, 0, n))
if len(result) == 0:
raise ValueError('data must have at least one element')
More information about the Python-checkins
mailing list