1

このようなリストの場合:

for i in range(100):
    things.append({'count':1})

for i in range(100):
    things.append({'count':2})

1リスト内の数を数えるには:

len([i['count'] for i in things if i['count'] == 1])

より良い方法は何ですか?

4

1 に答える 1

3

collections.Counter

>>> from collections import Counter
>>> c = Counter([thing['count'] for thing in things])
>>> c[1]               # Number of elements with count==1
100
>>> c[2]               # Number of elements with count==2
100
>>> c.most_common()    # Most common elements
[(1, 100), (2, 100)]
>>> sum(c.values())    # Number of elements
200
>>> list(c)            # List of unique counts
[1, 2]
>>> dict(c)            # Converted to a dict 
{1: 100, 2: 100}

おそらくあなたはこのようなことをすることができますか?

class DictCounter(object):
    def __init__(self, list_of_ds):
        for k,v in list_of_ds[0].items():
            self.__dict__[k] = collections.Counter([d[k] for d in list_of_ds])

>>> new_things = [{'test': 1, 'count': 1} for i in range(10)]
>>> for i in new_things[0:5]: i['count']=2

>>> d = DictCounter(new_things)
>>> d.count
Counter({1: 5, 2: 5})
>>> d.test
Counter({1: 10})

欠落しているキーを処理するための拡張DictCounter:

>>> class DictCounter(object):
    def __init__(self, list_of_ds):
        keys = set(itertools.chain(*(i.keys() for i in list_of_ds)))
        for k in keys:
            self.__dict__[k] = collections.Counter([d.get(k) for d in list_of_ds])

>>> a = [{'test': 5, 'count': 4}, {'test': 3, 'other': 5}, {'test':3}, {'test':5}]
>>> d = DictCounter(a)
>>> d.test
Counter({3: 2, 5: 2})
>>> d.count
Counter({None: 3, 4: 1})
>>> d.other
Counter({None: 3, 5: 1})
于 2012-12-21T02:50:56.367 に答える