Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 字典列表中每个键的总和_Django_Python 3.x - Fatal编程技术网

Django 字典列表中每个键的总和

Django 字典列表中每个键的总和,django,python-3.x,Django,Python 3.x,我有一个来自Django查询集的字典列表 像这样: 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':

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

像这样:

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
  }
]
现在,我要分别计算每个键的和

像这样:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value
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'))
inital_contact=3
第一次跟进=2
第二次跟进=3
第三次跟踪=2

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

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))
但现在,我在所有字典中都得到了11个键,并且我正在实现11个lambda函数,所以有没有什么好的方法来解决这个问题,而不是调用11次lambda函数

通过遵循ORM,我收到的电子邮件数量超过

i = Q(inital_contact__range=(from_date, to_date))
f1 = Q(first_followup__range=(from_date, to_date))
f2 = Q(second_followup__range=(from_date, to_date))
f3 = Q(third_followup__range=(from_date, to_date))

email_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'),
                                                             ).values('initial_count', 'first_follow_count',
                                                                      'second_follow_count', 'third_follow_count'

那么,有没有一种直接使用ORM的解决方案?

如果您不介意得到一本字典,您可以这样使用:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value
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'))
导致

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

如果你不介意得到一本字典,你可以这样使用:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value
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'))
导致

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

或者,如果您希望自己不使用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}

或者,如果您希望自己不使用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}

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

ORM如下所示:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value
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}
所以,没有for循环,没有lambda。。。我认为在性能方面,它会起作用


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

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

ORM如下所示:

from collections import defaultdict

sums = defaultdict(int)
for item in email_sent_count:
    for key, value in item.items():
        sums[key] += value
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}
所以,没有for循环,没有lambda。。。我认为在性能方面,它会起作用


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

现在所有的答案都适用于dicts,但更好(更快)的方法是在数据库中进行编辑。添加了ORM查询。。!!现在所有的答案都适用于dicts,但更好(更快)的方法是在数据库中进行编辑。添加了ORM查询。。!!谢谢这对我很管用。。!!我还添加了ORM查询。有嵌套循环,但我有大量的数据,这将减慢进程,所以,如果ORM可以改变,它的甲烷好。。。这对我很管用。。!!我还添加了ORM查询。有嵌套循环,但我有大量的数据,这会减慢进程,所以,如果可以更改ORM,这对我很好