['a', 'a', 'b', 'b', 'b']
,[('a', 2), ('b', 3)]
I want to I want to take only two or more from there.
SQL seems easy. Is it something that can't be done with Python alone?
Made with
[(g[0], len(list(g[1]))) for g in itertools.groupby(array.sort())]
Example
>>> import itertools
>>> array = ['a', 'a', 'b', 'b', 'b']
>>> uniq = [(g[0], len(list(g[1]))) for g in itertools.groupby(array.sort())]
>>> uniq
[('a', 2), ('b', 3)]
>>> tuples = [('aaa', 'bbb'), ('aaa', 'bbb'), ('aaa', 'vvv'), ('bbb', 'ccc'), ('bbb', 'ccc')]
>>> uniq = [(g[0], len(list(g[1]))) for g in itertools.groupby(tuples.sort())]
>>> uniq
[(('aaa', 'bbb'), 2), (('aaa', 'vvv'), 1), (('bbb', 'ccc'), 2)]
>>> filter(lambda f: f[1] > 1, uniq)
[(('aaa', 'bbb'), 2), (('bbb', 'ccc'), 2)]
Only two or more were pulled out.
http://stackoverflow.com/questions/2392929/how-to-get-unique-values-with-respective-occurance-count-from-a-list-in-python
I got a comment about a more useful library.
collections
>>> from collections import Counter
>>> tuples = [('aaa', 'bbb'), ('aaa', 'bbb'), ('aaa', 'vvv'), ('bbb', 'ccc'), ('bbb', 'ccc')]
>>> uniq = Counter(tuples)
>>> uniq
Counter({('aaa', 'bbb'): 2, ('bbb', 'ccc'): 2, ('aaa', 'vvv'): 1})
>>> filter(lambda f: f[1] > 1, uniq.items())
[(('aaa', 'bbb'), 2), (('bbb', 'ccc'), 2)]
collections.Let's use Counter!!
# reference
http://docs.python.jp/2/library/collections.html
Recommended Posts