]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
statistics.fmean(): speed-up code path for non-sizeable inputs. (gh-119876) 119460/head
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Fri, 31 May 2024 22:08:55 +0000 (17:08 -0500)
committerGitHub <noreply@github.com>
Fri, 31 May 2024 22:08:55 +0000 (17:08 -0500)
Lib/statistics.py

index c2f4fe8e054d3ddf37c1a0fe50528f6352dc57e1..450edfaabe8defddda055e7365335bd79639d6d4 100644 (file)
@@ -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