Skip to content

Commit fbc7676

Browse files
authored
Speed up counting in statistics.fmean() (gh-148875)
1 parent 8bf99ae commit fbc7676

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

Lib/statistics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136

137137
from fractions import Fraction
138138
from decimal import Decimal
139-
from itertools import count, groupby, repeat
139+
from itertools import compress, count, groupby, repeat
140140
from bisect import bisect_left, bisect_right
141141
from math import hypot, sqrt, fabs, exp, erfc, tau, log, fsum, sumprod
142142
from math import isfinite, isinf, pi, cos, sin, tan, cosh, asin, atan, acos
@@ -195,9 +195,9 @@ def fmean(data, weights=None):
195195
n = len(data)
196196
except TypeError:
197197
# Handle iterators that do not define __len__().
198-
counter = count()
199-
total = fsum(map(itemgetter(0), zip(data, counter)))
200-
n = next(counter)
198+
counter = count(1)
199+
total = fsum(compress(data, counter))
200+
n = next(counter) - 1
201201
else:
202202
total = fsum(data)
203203

0 commit comments

Comments
 (0)