Skip to content

Commit 6c01ebc

Browse files
authored
bpo-37158: Simplify and speed-up statistics.fmean() (GH-13832)
1 parent ccf0efb commit 6c01ebc

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Lib/statistics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ def fmean(data):
320320
except TypeError:
321321
# Handle iterators that do not define __len__().
322322
n = 0
323-
def count(x):
323+
def count(iterable):
324324
nonlocal n
325-
n += 1
326-
return x
327-
total = fsum(map(count, data))
325+
for n, x in enumerate(iterable, start=1):
326+
yield x
327+
total = fsum(count(data))
328328
else:
329329
total = fsum(data)
330330
try:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed-up statistics.fmean() by switching from a function to a generator.

0 commit comments

Comments
 (0)