changeset 455:abe0b2347d4c

added most common utility function
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 18 Feb 2014 14:52:49 -0500
parents 62d05436099d
children 825e5d49325d
files python/tests/utils.txt python/utils.py
diffstat 2 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python/tests/utils.txt	Thu Feb 13 16:42:32 2014 -0500
+++ b/python/tests/utils.txt	Tue Feb 18 14:52:49 2014 -0500
@@ -44,6 +44,15 @@
 >>> stepPlot([3, 5, 7, 8], 1, 10, 0)
 ([1, 3, 3, 5, 5, 7, 7, 8, 8, 10], [0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
 
+>>> mostCommon(['a','b','c','b'])
+'b'
+>>> mostCommon(['a','b','c','b', 'c'])
+'b'
+>>> mostCommon(range(10)+[1])
+1
+>>> mostCommon([range(2), range(4), range(2)])
+[0, 1]
+
 >>> lcss = LCSS(lambda x,y: abs(x-y) <= 0.1)
 >>> lcss.compute(range(5), range(5))
 5
--- 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
 #########################