Skip to content

Commit

Permalink
statistics.fmean(): speed-up code path for non-sizeable inputs. (pyth…
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger authored May 31, 2024
1 parent d28afd3 commit cc5cd4d
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 cc5cd4d

Please sign in to comment.