Skip to content

Commit

Permalink
Speed-up code path for non-sizeable inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed May 31, 2024
1 parent 9bc6045 commit b6a09cc
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,11 @@ def fmean(data, weights=None):
n = len(data)
except TypeError:
# Handle iterators that do not define __len__().
n = 0
def count(iterable):
nonlocal n
for n, x in enumerate(iterable, start=1):
yield x
data = count(data)
total = fsum(data)
counter = count()
total = fsum(map(itemgetter(0), zip(data, counter)))
n = next(counter)
else:
total = fsum(data)
if not n:
raise StatisticsError('fmean requires at least one data point')
return total / n
Expand Down

0 comments on commit b6a09cc

Please sign in to comment.