0

I need to add items to a counter and needs to be more dynamic.

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

For example, then last element in the list of hashes does not contain region. Yet the below is how I add elements and will get an error.

C = Counter()
for item in hash_data:
    C[item['utcdt'],item['campaign_id'], item['country'], item['region']] += item[metric]

Ideally something like this but of course not work

C = Counter()
for item in hash_data:
    m1 = item.keys()
    m2 = []
    for i in ml:
        if i!='impression':
           ms.add(i)
    C[ml] += item[metric]
Tampa
  • 75,446
  • 119
  • 278
  • 425

2 Answers2

1
C = Counter()
for item in hash_data:
  C[tuple(item.values())] += item[metric]
Vlad
  • 18,195
  • 4
  • 41
  • 71
  • Not a good idea, because two dictionaries may have different key subsets but the same values – Eric Dec 21 '13 at 23:44
0

As I mentioned in the comments, you can use the dict.get with a predefined default value which will be returned in case the key is not present in the dict.

default_region = 'default_region'
...
...
item.get('region', default_region)

If you have many keys which you think might be absent from your dict, you can use collections.defaultdict if that suits your purpose (See this discussion). Alternatively, you can simply create a dict of default values containing all possible keys and their corresponding default values. A small example could be:

defaults = {
            'campaign_id': 'default_campaign_id',
            'country': 'default_country',
            'impressions': -1,
            'region': 'default_region',
            'utcdt': datetime.datetime.min,
           }

for item in hash_data:
    C[tuple(item.get(k, dv) for k, dv in defaults.items() if k != metric)] += item.get(metric, defaults[metric])

HTH!

Community
  • 1
  • 1
mg007
  • 2,888
  • 24
  • 29