Skip to content

Commit cc5cd4d

Browse files
authored
statistics.fmean(): speed-up code path for non-sizeable inputs. (gh-119876)
1 parent d28afd3 commit cc5cd4d

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

Diff for: Lib/statistics.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,11 @@ def fmean(data, weights=None):
505505
n = len(data)
506506
except TypeError:
507507
# Handle iterators that do not define __len__().
508-
n = 0
509-
def count(iterable):
510-
nonlocal n
511-
for n, x in enumerate(iterable, start=1):
512-
yield x
513-
data = count(data)
514-
total = fsum(data)
508+
counter = count()
509+
total = fsum(map(itemgetter(0), zip(data, counter)))
510+
n = next(counter)
511+
else:
512+
total = fsum(data)
515513
if not n:
516514
raise StatisticsError('fmean requires at least one data point')
517515
return total / n

0 commit comments

Comments
 (0)