字典python列表中每个键的值和

时间:2018-03-29 09:28:55

标签: django python-3.x

我有一个来自Django查询集的字典列表。

像这样:

conditions = ('A','V','W')
df = df.query('ColA != "%s"' % conditions)

现在,我想分别计算每个密钥的总和。

像这样:

email_sent_count = [ { 'second_follow_count': 1, 'first_follow_count': 1, 'initial_count': 1, 'third_follow_count': 0 }, { 'second_follow_count': 1, 'first_follow_count': 0, 'initial_count': 1, 'third_follow_count': 1 }, { 'second_follow_count': 1, 'first_follow_count': 1, 'initial_count': 1, 'third_follow_count': 1 } ]

我正在尝试以下解决方案:

inital_contact = 3
first_followup = 2
second_followup = 3
third_followup = 2

但是现在,我在所有字典中都获得了11个键,并且我正在实现11个lambda函数,所以有什么好方法可以解决这个问题而不是调用11次lambda函数

通过关注ORM,我获得了email_sent_count

initial_contact = sum(map(lambda x: x['initial_count'], email_sent_count))
first_followup = sum(map(lambda x: x['first_follow_count'], email_sent_count))
second_followup = sum(map(lambda x: x['second_follow_count'], email_sent_count))
third_followup = sum(map(lambda x: x['third_follow_count'], email_sent_count))

那么,是否有一个直接与ORM合作的解决方案?

3 个答案:

答案 0 :(得分:4)

如果您不介意获取字典作为结果,您可以使用collections.defaultdict,如下所示:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value

导致

defaultdict(<class 'int'>, 
            {'second_follow_count': 3, 'initial_count': 3, 
             'first_follow_count': 2, 'third_follow_count': 2})

您可以像字典一样访问各个总和:sums['second_follow_count']

......或者collections.Counter可能更好:

from collections import Counter

sums = Counter()
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value

# Counter({'second_follow_count': 3, 'initial_count': 3,
#          'first_follow_count': 2, 'third_follow_count': 2})

答案 1 :(得分:0)

或者,如果您愿意,不使用Counter或DefaultDict自己动手:

from pprint import pprint
email_sent_count = [
  {
    'second_follow_count': 1,
    'first_follow_count': 1,
    'initial_count': 1,
    'third_follow_count': 0
  },
  {
    'second_follow_count': 1,
    'first_follow_count': 0,
    'initial_count': 1,
    'third_follow_count': 1
  },
  {
    'second_follow_count': 1,
    'first_follow_count': 1,
    'initial_count': 1,
    'third_follow_count': 1
  }
]

# create empty dict with all keys from any inner dict and initialized to 0
alls  = dict( (x,0) for y in email_sent_count for x in y)  
pprint(alls)

for d in email_sent_count:
    for k in d:
        alls[k] += d[k]

pprint(alls)

输出:

{'first_follow_count': 0,
 'initial_count': 0,
 'second_follow_count': 0,
 'third_follow_count': 0}

{'first_follow_count': 2,
 'initial_count': 3,
 'second_follow_count': 3,
 'third_follow_count': 2}

答案 2 :(得分:0)

最后,我更改了获得确切结果的ORM查询:

ORM看起来像这样:

email_sent_count = campaign_contact.filter(i | f1 | f2 | f3
                                      ).annotate(initial_count=Count('inital_contact'),
                                                 first_follow_count=Count('first_followup'),
                                                 second_follow_count=Count('second_followup'),
                                                 third_follow_count=Count('third_followup')
                                                 ).aggregate(initial_sum=Sum('initial_count'),
                                                             first_follow_sum=Sum('first_follow_count'),
                                                             second_follow_sum=Sum('second_follow_count'),
                                                             third_follow_sum=Sum('third_follow_count'))

...输出

{'third_follow_sum': 2, 'second_follow_sum': 3, 'first_follow_sum': 2, 'initial_sum': 3}

所以,没有循环,没有lambda ...我认为性能明智,它会起作用。

感谢大家为我提供解决方案,通过寻找其他解决方案,我能够解决这个问题。 :)