From 0fdcaaf281151bb0f6e1620b54400ca73068f6eb Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Mon, 5 Jan 2015 17:38:47 +0100 Subject: [PATCH] Optimise beets.util.plurality() Shorter & more idiomatic code: rely on collections.Counter() --- beets/util/__init__.py | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index ff9192abb..bae3ece25 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -22,7 +22,7 @@ import sys import re import shutil import fnmatch -from collections import defaultdict +from collections import Counter import traceback import subprocess import platform @@ -588,27 +588,14 @@ def levenshtein(s1, s2): def plurality(objs): - """Given a sequence of comparable objects, returns the object that - is most common in the set and the frequency of that object. The + """Given a sequence of hashble objects, returns the object that + is most common in the set and the its number of appearance. The sequence must contain at least one object. """ - # Calculate frequencies. - freqs = defaultdict(int) - for obj in objs: - freqs[obj] += 1 - - if not freqs: + c = Counter(objs) + if not c: raise ValueError('sequence must be non-empty') - - # Find object with maximum frequency. - max_freq = 0 - res = None - for obj, freq in freqs.items(): - if freq > max_freq: - max_freq = freq - res = obj - - return res, max_freq + return c.most_common(1)[0] def cpu_count():