]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Speed up counting in statistics.fmean() (gh-148875)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 23 Apr 2026 03:06:56 +0000 (22:06 -0500)
committerGitHub <noreply@github.com>
Thu, 23 Apr 2026 03:06:56 +0000 (22:06 -0500)
Lib/statistics.py

index e635b99f958e4413b708b5776b6c4a537a71426a..32fcf2313a815a11d382f5e3eb26f02532979577 100644 (file)
@@ -136,7 +136,7 @@ import sys
 
 from fractions import Fraction
 from decimal import Decimal
-from itertools import count, groupby, repeat
+from itertools import compress, count, groupby, repeat
 from bisect import bisect_left, bisect_right
 from math import hypot, sqrt, fabs, exp, erfc, tau, log, fsum, sumprod
 from math import isfinite, isinf, pi, cos, sin, tan, cosh, asin, atan, acos
@@ -195,9 +195,9 @@ def fmean(data, weights=None):
             n = len(data)
         except TypeError:
             # Handle iterators that do not define __len__().
-            counter = count()
-            total = fsum(map(itemgetter(0), zip(data, counter)))
-            n = next(counter)
+            counter = count(1)
+            total = fsum(compress(data, counter))
+            n = next(counter) - 1
         else:
             total = fsum(data)