Can Python lists understand (ideally) the equivalent of "count (*) ... group by ..." in SQL?

I think that understanding lists can give me this, but I'm not sure: are there any elegant solutions in Python (2.6) to select unique objects in a list and provide an account?

(I determined __eq__to determine the uniqueness of the definition of my object).

So, in RDBMS-land, something like this:

CREATE TABLE x(n NUMBER(1));
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(1);
INSERT INTO x VALUES(2);

SELECT COUNT(*), n FROM x
GROUP BY n;

What gives:

COUNT(*) n
==========
3        1
1        2

So here is my equivalent list in Python:

[1,1,1,2]

And I want to get the same result as SQL SELECT.

EDIT: The example I cited here has been simplified, I actually process lists of custom instance objects: just for completeness, I include the extra code that I need to make it all work:

import hashlib

def __hash__(self):
    md5=hashlib.md5()
    [md5.update(i) for i in self.my_list_of_stuff]
    return int(md5.hexdigest(),16)

set (set), __hash__ ( , 2.6 [ , , (. ) - , ]). my_list_of_stuff - () .

+5
5

, , :

>>> values = [1,1,1,2]
>>> print [(x,values.count(x)) for x in set(values)]
[(1, 3), (2, 1)]

S.Lott, defaultdict .

+11
>>> from collections import Counter
>>> Counter([1,1,1,2])
Counter({1: 3, 2: 1})

Counter, py3.1, dict.

+11

.

from collections import defaultdict
def group_by( someList ):
    counts = defaultdict(int)
    for value in someList:
        counts[value.aKey] += 1
    return counts

Pythonic . .

+6

groupby itertools:

, . - , . , . , .

>>> a = [1,1,1,2]
>>> [(len(list(v)), key) for (key, v) in itertools.groupby(sorted(a))]
[(3, 1), (1, 2)]

, , dict SilentGhost S.Lott, , . . , , . sorted , .

+4

Python 2.4 Python 2.6:

lst = [1,1,2,2,3,4,5,6,5]
lst_tmp = []
lst_dups = []

for item in lst:
    if item in lst_tmp:
        lst_dups.append(item)
    else:
        lst_tmp.append(item)

if len(lst_dups):
    lst_dups = sorted(set(lst_dups))
    for item in lst_dups:
        print str(lst.count(item)), "instances of", item
else:
    print "list is unique"
+1

All Articles