简体   繁体   中英

Compare multiple dictionaries for entry based on two values within dictionary

I have two python dictionaries, and I need an efficient way to iterate over one dictionary checking multiple values per-entry against another dictionary. If the entry does not exist I need to add that entry from one dictionary to the source dictionary.

I have tried different methods such as itter() over the two dictionary values and using "in" to see if the value exists but the logic is broken somewhere.

for key, value in export_data.iteritems():
  if  value['computer_id'] and value['computer_name'] in import_data.iteritems():

If computer_id and computer_name are in import_data then continue and if not then add the missing dictionary item from import_data to the export_data dictionary.

In the example data below ' import_data ' has a extra item " host-c " and I would like that added to the export_data dictionary.

The part I am having a hard time with is there is also a record for "host-g" but the computer_id is the same as host-a in export_data so I don't want to add that entry.

Only unique items where both computer_name and computer_id are different.

Example Data:

export_data = 
{u'host-a': {'computer_id': [u'6353a65387'], 'computer_name': ['host-a'], 'first_observed': ['Wed Jul 24 13:57:56  2019']}, {u'host-b': {'computer_id': [u'635365d387'], 'computer_name': ['host-b'], 'first_observed': ['Wed Jul 24 13:57:56  2019']}

import_data = 
{u'host-a': {'computer_id': [u'6353a65387'], 'computer_name': ['host-a'], 'first_observed': ['Wed Jul 24 13:57:56  2019']}, {u'host-b': {'computer_id': [u'635365d387'], 'computer_name': ['host-b'], 'first_observed': ['Wed Jul 24 13:57:56  2019']},{u'host-c': {'computer_id': [u'6353654d387'], 'computer_name': ['host-c'], 'first_observed': ['Wed Jul 24 13:57:56  2019']},{u'host-g': {'computer_id': [u'6353a65387'], 'computer_name': ['host-g'], 'first_observed': ['Wed Jul 24 13:57:56  2019']}

I don't think there is a way around iterating through both dicts and comparing the values manually. Experimenting with your code I got this:

export_data = {
    u'host-a': {
        'computer_id': [u'6353a65387'],
        'computer_name': ['host-a'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        },
    u'host-b': {
        'computer_id': [u'635365d387'],
        'computer_name': ['host-b'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        }
    }

import_data = {
    u'host-a': {
        'computer_id': [u'6353a65387'],
        'computer_name': ['host-a'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        },
    u'host-b': {
        'computer_id': [u'635365d387'],
        'computer_name': ['host-b'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        },
    u'host-c': {
        'computer_id': [u'6353654d387'],
        'computer_name': ['host-c'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        },
    u'host-g': {
        'computer_id': [u'6353a65387'],
        'computer_name': ['host-g'],
        'first_observed': ['Wed Jul 24 13:57:56  2019']
        }
    }

print(export_data)

additional_export_data = []

for im_key, im_value in import_data.items():
    found_in_export = False

    for ex_key, ex_value in export_data.items():
        if ex_value['computer_id'][0] == im_value['computer_id'][0] and ex_value['computer_name'][0] == im_value['computer_name'][0]:
            found_in_export = True

    if not found_in_export:
        additional_export_data.append((im_key, im_value))

        print(im_key, im_value['computer_id'][0])
        print("")

for key, value in additional_export_data:
    export_data[key] = value

print(export_data)

This adds key-value-pairs from import_data to export_data if there is no entry in export_data with the same computer_id and computer_name .


Also I don't know why your export_data and import_data structured the way they are. The keys of the dictionaries are the same as the computer_name values. The computer_id , computer_name and first_observed are each a list even though it doesn't make sense to me.

Maybe you have a reason why you structured your data this way but if not I would recommend doing the following:

  • get rid of computer_name in the sub dictionaries
  • store the values of computer_id and first_observed just as string, not as a string in a list

If you do this, the code looks like this:

export_data = {
    u'host-a': {
        'computer_id': u'6353a65387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        },
    u'host-b': {
        'computer_id': u'635365d387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        }
    }

import_data = {
    u'host-a': {
        'computer_id': u'6353a65387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        },
    u'host-b': {
        'computer_id': u'635365d387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        },
    u'host-c': {
        'computer_id': u'6353654d387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        },
    u'host-g': {
        'computer_id': u'6353a65387',
        'first_observed': 'Wed Jul 24 13:57:56  2019'
        }
    }

print(export_data)

additional_export_data = []

for im_key, im_value in import_data.items():
    found_in_export = False

    for ex_key, ex_value in export_data.items():
        if ex_value['computer_id'] == im_value['computer_id'] and ex_key == im_key:
            found_in_export = True

    if not found_in_export:
        additional_export_data.append((im_key, im_value))

        print(im_key, im_value['computer_id'])
        print("")

for key, value in additional_export_data:
    export_data[key] = value

print(export_data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM