bpo-45851: Avoid full sort in statistics.multimode() (#29662)

Suggested by Stefan Pochmann.
This commit is contained in:
Raymond Hettinger 2021-11-20 10:04:37 -06:00 committed by GitHub
parent ef5305819f
commit 04e03f496c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -609,9 +609,11 @@ def multimode(data):
>>> multimode('') >>> multimode('')
[] []
""" """
counts = Counter(iter(data)).most_common() counts = Counter(iter(data))
maxcount, mode_items = next(groupby(counts, key=itemgetter(1)), (0, [])) if not counts:
return list(map(itemgetter(0), mode_items)) return []
maxcount = max(counts.values())
return [value for value, count in counts.items() if count == maxcount]
# Notes on methods for computing quantiles # Notes on methods for computing quantiles