erika
erika

Reputation: 7

Python update method

I have been looking how to achieve that, but Im really missing something there as I can't find any solution. Im trying to group all get_campaign_result['data'] keys with empty values, grouped by get_campaign_result['campaignId']

Any idea would help, thank you so much

get_campaign_result =  [{'id': '549972d5c469885e548b4577',
  'campaignId': '5499612ec4698839368b4573',
  'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  'location': 'Amsterdam, Netherlands',
  'date': '2014-01-15T19:48:06.003Z',
  'customData': {'form_name': 'form1'},
  'data': {'text': 'test'},
  'time': 5000,
  'url': 'https://usabilla.com'},
 {'id': '549972d5c469885e548b4570',
  'campaignId': '5499612ec4698839368b4573',
  'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  'location': 'Amsterdam, Netherlands',
  'date': '2014-01-15T19:48:06.003Z',
  'customData': {'form_name': 'form1'},
  'data': {'text': 'test'},
  'time': 5000,
  'url': 'https://usabilla.com'},
 {'id': '549972d5c469885e548b4575',
  'campaignId': '5499612ec4698839368b4573',
  'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  'location': 'Amsterdam, Netherlands',
  'date': '2014-01-15T19:48:06.003Z',
  'customData': {'form_name': 'form1'},
  'data': {'uuuu': 'test'},
  'time': 5000,
  'url': 'https://usabilla.com'},
 {'id': '549972d5c469885e548b4522',
  'campaignId': '5499612ec4698839368b4578',
  'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  'location': 'Amsterdam, Netherlands',
  'date': '2014-01-15T19:48:06.003Z',
  'customData': {'form_name': 'form1'},
  'data': {'text2': 'test'},
  'time': 5000,
  'url': 'https://usabilla.com'},
 {'id': '549972d5c469885e548b4533',
  'campaignId': '5499612ec4698839368b4578',
  'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
  'location': 'Amsterdam, Netherlands',
  'date': '2014-01-15T19:48:06.003Z',
  'customData': {'form_name': 'form1'},
  'data': {'text__': 'test'},
  'time': 5000,
  'url': 'https://usabilla.com'}]

MY CODE

structure= {}
    for campaignresult in get_campaign_result:
        custom_element  = campaignresult['data']
        campaign_id = campaignresult['campaignId']
        structure[campaign_id] = {}
        for element in custom_element:
            d = {element:''}
            print(structure)
            structure[campaign_id].update(d)

result i get

{'5499612ec4698839368b4573': {'uuuu': ''},
 '5499612ec4698839368b4578': {'text__': ''}}

result I'am expecting

{'5499612ec4698839368b4573': {'text': '', 'uuuu': ''},
 '5499612ec4698839368b4578': {'text2': '', 'text__': ''}}

Upvotes: 0

Views: 79

Answers (4)

mhawke
mhawke

Reputation: 87084

A nice way to handle grouping and collating of data is to use a defaultdict. Set it up so that its values are dicts.

from collections import defaultdict

structure = defaultdict(dict)

for campaignresult in get_campaign_result:
    structure[campaignresult['campaignId']].update({k:'' for k in campaignresult['data']})


>>> from pprint import pprint
>>> pprint(structure)
defaultdict(<class 'dict'>,
            {'5499612ec4698839368b4573': {'text': '', 'uuuu': ''},
             '5499612ec4698839368b4578': {'text2': '', 'text__': ''}})

>>> pprint(dict(structure))    # convert to standard dict if desired
{'5499612ec4698839368b4573': {'text': '', 'uuuu': ''},
 '5499612ec4698839368b4578': {'text2': '', 'text__': ''}}

Upvotes: 0

Hugo G
Hugo G

Reputation: 16496

There are two problems.

First, you want to save the whole custom_element but you iterate over its elements instead.

Second, the more important problem, the expected data structure as you described it is not possible in Python. Each dictionary key must be unique. Your options are either a list of of campaign to data mappings, or a mapping of campaign to a list of data dicts.

Here is an (untested) example of the latter:

structure = {}
for campaignresult in get_campaign_result:
    data  = campaignresult['data']
    campaign_id = campaignresult['campaignId']
    
    if "" not in data.values():
        # none of the data fields are empty. skip
        continue

    # If there is no list for this campaign yet, create one as a
    # value on the `structure` dict. Otherwise return the existing one.
    campaign_data_list = structure.setdefault(campaign_id, [])

    # Add a new entry to the list with data of the current campaign
    campaign_data_list.append(data)

print(structure)        

Upvotes: 0

Dzianis Talkachou
Dzianis Talkachou

Reputation: 436

Whenever you get campaign_id from campaignresult, you create new empty dictionary and all old values are lost if they were. You only need to create new dictionary key once (if this key is not in dictionary). See line 5.

structure= {}
for campaignresult in get_campaign_result:
    custom_element  = campaignresult['data']
    campaign_id = campaignresult['campaignId']
    if campaign_id not in structure:
      structure[campaign_id] = {}
    for element in custom_element:
        d = {element: ''}
        structure[campaign_id].update(d)

Upvotes: 1

Joao Pedro Barreto
Joao Pedro Barreto

Reputation: 83

I am not sure about the answer in terms of code, but I think the result you are expecting is not possible. You have a dictionary with two keys: 5499612ec4698839368b4573 and 5499612ec4698839368b4578. These two are possible.

However, these two ({'uuuu': ''} and '{text__': ''}) are not. Every item in a dictionary must have a key. In this case, both these items have no keys on the parent dict. You might be mixing the list and dictionary syntaxes.

Hope this helps you understand better the problem!

Upvotes: 1

Related Questions