statistics.fmean(): speed-up code path for non-sizeable inputs. (gh-119876)

This commit is contained in:
Raymond Hettinger 2024-05-31 17:08:55 -05:00 committed by GitHub
parent d28afd3fa0
commit cc5cd4d93e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 7 deletions

View 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