mardi 14 juin 2016

Updating a Dictionary from another dictionary

So I have a dictionary, Which is something like this.

data1 = {"00:08:22:24:f8:02": {
            "mac_address" : "00:08:22:24:f8:02",
            "cities" : 
                   [
                     {'name': 'Bhubaneswar', 'count': 12},
                     {'name': 'Kolkata', 'count': 4},
                     {'name': 'Mumbai', 'count': 6}
                ]
     }

}

and one more dictionary.

data2 = {'00:03:7f:05:c0:06': {'cities': [{'count': 1, 'name': 'Kolkata'}],
                   'mac_address': '00:03:7f:05:c0:06'},
'00:08:22:1c:50:07': {'cities': [{'count': 1, 'name': 'Bhubaneswar'},
                              {'count': 1, 'name': 'Kolkata'},
                              {'count': 2, 'name': 'Mumbai'}],
                   'mac_address': '00:08:22:1c:50:07'},
'00:08:22:24:cc:fb': {'cities': [{'count': 1, 'name': 'Bhubaneswar'}],
                   'mac_address': '00:08:22:24:cc:fb'},
'00:08:22:24:f8:02': {'cities': [{'count': 1, 'name': 'Bhubaneswar'},
                              {'count': 1, 'name': 'Kolkata'},
                              {'count': 2, 'name': 'Mumbai'}],
                   'mac_address': '00:08:22:24:f8:02'}}

I want to update my dictionary 1 from the dictionary 2 so that for each mac_address which is the key of dictionary. The values of city and city count get updated. If some mac_address is not there in dict1 and it is there in 2nd dictionary I want to add that mac_address as new key value pair in my dictionary. Values for one unique mac_address is needed to be combined. All this i need to do in python.

Aucun commentaire:

Enregistrer un commentaire