diff python/utils.py @ 455:abe0b2347d4c

added most common utility function
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 18 Feb 2014 14:52:49 -0500
parents 9a714f32fc9f
children 825e5d49325d
line wrap: on
line diff
--- a/python/utils.py	Thu Feb 13 16:42:32 2014 -0500
+++ b/python/utils.py	Tue Feb 18 14:52:49 2014 -0500
@@ -269,6 +269,31 @@
     return coef
 
 #########################
+# iterable section
+#########################
+
+def mostCommon(L):
+    '''Returns the most frequent element in a iterable'''
+    from itertools import groupby
+    from operator import itemgetter
+    # get an iterable of (item, iterable) pairs
+    SL = sorted((x, i) for i, x in enumerate(L))
+    # print 'SL:', SL
+    groups = groupby(SL, key=itemgetter(0))
+    # auxiliary function to get "quality" for an item
+    def _auxfun(g):
+        item, iterable = g
+        count = 0
+        min_index = len(L)
+        for _, where in iterable:
+            count += 1
+            min_index = min(min_index, where)
+            # print 'item %r, count %r, minind %r' % (item, count, min_index)
+        return count, -min_index
+    # pick the highest-count/earliest item
+    return max(groups, key=_auxfun)[0]
+
+#########################
 # sequence section
 #########################