如何在哈希列表中组合哈希?

2024-04-23 06:57:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个哈希列表,如下所示:

   [{'campaign_id': 'cid2504649263',
  'country': 'AU',
  'impressions': 3000,
  'region': 'Cairns',
  'utcdt': datetime.datetime(2013, 6, 4, 6, 0)},
 {'campaign_id': 'cid2504649263',
  'country': 'AU',
  'count': 9000,
  'region': 'Cairns',
  'utcdt': datetime.datetime(2013, 6, 4, 6, 0)},
 {'campaign_id': 'cid2504649263',
  'country': 'AU',
  'count': 3000,
  'region': 'Cairns',
  'utcdt': datetime.datetime(2013, 6, 4, 7, 0)}]

有两个哈希值需要上卷,因为所有维度都是相同的,我需要求和计数。那么……我如何在itertools中使用python groupby来完成这项任务呢?还有别的办法吗?

^{pr2}$

Tags: id列表datetimecountcountryregion计数au
2条回答

如果需要汇总的项目是连续的,那么groupby就可以了。否则你需要先把它们分类。我认为一个collections.Counter会对你更好

>>> import datetime
>>> from collections import Counter
>>> C = Counter()
>>> L =     [{'campaign_id': 'cid2504649263',
...   'country': 'AU',
...   'count': 3000,            # <== changed this to "count"
...   'region': 'Cairns',
...   'utcdt': datetime.datetime(2013, 6, 4, 6, 0)},
...  {'campaign_id': 'cid2504649263',
...   'country': 'AU',
...   'count': 3000,
...   'region': 'Cairns',
...   'utcdt': datetime.datetime(2013, 6, 4, 6, 0)},
...  {'campaign_id': 'cid2504649263',
...   'country': 'AU',
...   'count': 3000,
...   'region': 'Cairns',
...   'utcdt': datetime.datetime(2013, 6, 4, 7, 0)}]
>>> for item in L:                        # The ... represents the rest of the key
...     C[item['campaign_id'], item['country'], ...,  item['utcdt']] += item['count']
...
C
Counter({('cid2504649263', 'AU', datetime.datetime(2013, 6, 4, 6, 0)): 6000, ('cid2504649263', 'AU', datetime.datetime(2013, 6, 4, 7, 0)): 3000})

然后将计数器转换回列表格式

There are two hashes that need to be rolled up because all of the dimensions are same and I need to sum count.

如果这就是你想要的,那么:

from collections import defaultdict

d = defaultdict(int)

for i in hashes:
   d[i['campaign_id'],i['region']] += i['count']

for k in d:
    print k[0],d[k]

相关问题 更多 >