mirror of
https://github.com/python/cpython.git
synced 2025-09-19 07:00:59 +00:00
statistics.fmean(): speed-up code path for non-sizeable inputs. (gh-119876)
This commit is contained in:
parent
d28afd3fa0
commit
cc5cd4d93e
1 changed files with 5 additions and 7 deletions
|
@ -505,13 +505,11 @@ def fmean(data, weights=None):
|
||||||
n = len(data)
|
n = len(data)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# Handle iterators that do not define __len__().
|
# Handle iterators that do not define __len__().
|
||||||
n = 0
|
counter = count()
|
||||||
def count(iterable):
|
total = fsum(map(itemgetter(0), zip(data, counter)))
|
||||||
nonlocal n
|
n = next(counter)
|
||||||
for n, x in enumerate(iterable, start=1):
|
else:
|
||||||
yield x
|
total = fsum(data)
|
||||||
data = count(data)
|
|
||||||
total = fsum(data)
|
|
||||||
if not n:
|
if not n:
|
||||||
raise StatisticsError('fmean requires at least one data point')
|
raise StatisticsError('fmean requires at least one data point')
|
||||||
return total / n
|
return total / n
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue