summaryrefslogtreecommitdiffstats
path: root/Lib/statistics.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2021-11-20 16:04:37 (GMT)
committerGitHub <noreply@github.com>2021-11-20 16:04:37 (GMT)
commit04e03f496cf7da48ce4f545b41579d7d45f59ad2 (patch)
tree5ae0640527f1632f4bb3b8364a47571a43ddc9e0 /Lib/statistics.py
parentef5305819ff9b283d92dbf004f977592f4e64165 (diff)
downloadcpython-04e03f496cf7da48ce4f545b41579d7d45f59ad2.zip
cpython-04e03f496cf7da48ce4f545b41579d7d45f59ad2.tar.gz
cpython-04e03f496cf7da48ce4f545b41579d7d45f59ad2.tar.bz2
bpo-45851: Avoid full sort in statistics.multimode() (#29662)
Suggested by Stefan Pochmann.
Diffstat (limited to 'Lib/statistics.py')
-rw-r--r--Lib/statistics.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/Lib/statistics.py b/Lib/statistics.py
index e67c517..4f3ab49 100644
--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -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