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('')
[]
"""
counts = Counter(iter(data)).most_common()
maxcount, mode_items = next(groupby(counts, key=itemgetter(1)), (0, []))
return list(map(itemgetter(0), mode_items))
counts = Counter(iter(data))
if not counts:
return []
maxcount = max(counts.values())
return [value for value, count in counts.items() if count == maxcount]
# Notes on methods for computing quantiles