8000 bpo-45851: Avoid full sort in statistics.multimode() (#29662) · python/cpython@04e03f4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04e03f4

Browse files
authored
bpo-45851: Avoid full sort in statistics.multimode() (#29662)
Suggested by Stefan Pochmann.
1 parent ef53058 commit 04e03f4

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Lib/statistics.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,11 @@ def multimode(data):
609609
>>> multimode('')
610610
[]
611611
"""
612-
counts = Counter(iter(data)).most_common()
613-
maxcount, mode_items = next(groupby(counts, key=itemgetter(1)), (0, []))
614-
return list(map(itemgetter(0), mode_items))
612+
counts = Counter(iter(data))
613+
if not counts:
614+
return []
615+
maxcount = max(counts.values())
616+
return [value for value, count in counts.items() if count == maxcount]
615617

616618

617619
# Notes on methods for computing quantiles

0 commit comments

Comments
 (0)
0