比较嵌套的Python dict与list和dict

时间:2018-06-20 14:25:24

标签: python git list dictionary

我见过类似的问题,但是没有一个问题与我正在做的事情完全吻合,我相信其他开发人员在使用MongoDB时可能会遇到同样的问题。

我正在寻找将两个嵌套的dict对象与dict和array进行比较,并返回一个带有添加和删除操作的dict(就像您将git diff两个文件混在一起)

这是我到目前为止所拥有的:

def dict_diff(alpha, beta, recurse_adds=False, recurse_dels=False):
    """
    :return: differences between two python dict with adds and dels
    example:
    (This is the expected output)
    {
        'adds': 
        {
            'specific_hours': [{'ends_at': '2015-12-25'}],
        }
        'dels': 
        {
            'specific_hours': [{'ends_at': '2015-12-24'}],
            'subscription_products': {'review_management': {'thiswillbedeleted': 'deleteme'}}
        }
    }
    """
    if type(alpha) is dict and type(beta) is dict:
        a_keys = alpha.keys()
        b_keys = beta.keys()
        dels = {}
        adds = {}
        for key in a_keys:
            if type(alpha[key]) is list:
                if alpha[key] != beta[key]:
                    adds[key] = dict_diff(alpha[key], beta[key], recurse_adds=True)
                    dels[key] = dict_diff(alpha[key], beta[key], recurse_dels=True)
            elif type(alpha[key]) is dict:
                if alpha[key] != beta[key]:
                    adds[key] = dict_diff(alpha[key], beta[key], recurse_adds=True)
                    dels[key] = dict_diff(alpha[key], beta[key], recurse_dels=True)
            elif key not in b_keys:
                dels[key] = alpha[key]
            elif alpha[key] != beta[key]:
                adds[key] = beta[key]
                dels[key] = alpha[key]

        for key in b_keys:
            if key not in a_keys:
                adds[key] = beta[key]

    elif type(alpha) is list and type(beta) is list:
        index = 0
        adds=[]
        dels=[]
        for elem in alpha:
            if alpha[index] != beta[index]:
                dels.append(alpha[index])
                adds.append(beta[index])
                # print('update', adds, dels)
            index+=1
    else:
        raise Exception("dict_diff function can only get dict objects")

    if recurse_adds:
        if bool(adds):
            return adds
        return {}
    if recurse_dels:
        if bool(dels):
            return dels
        return {}
    return {'adds': adds, 'dels': dels}

我现在得到的结果是:

{'adds': {'specific_hours': [{'ends_at': '2015-12-24',
                              'open_hours': ['07:30-11:30', '12:30-21:30'],
                              'starts_at': '2015-12-22'},
                             {'ends_at': '2015-01-03',
                              'open_hours': ['07:30-11:30'],
                              'starts_at': '2015-01-0'}],
          'subscription_products': {'review_management': {}}},
 'dels': {'specific_hours': [{'ends_at': '2015-12-24',
                              'open_hours': ['07:30-11:30', '12:30-21:30'],
                              'starts_at': '2015-12-2'},
                             {'ends_at': '2015-01-03',
                              'open_hours': ['07:30-11:30'],
                              'starts_at': '2015-01-0'}],
          'subscription_products': {'review_management': {'thiswillbedeleted': 'deleteme'}}}}

这是我要比较的两个对象:

alpha = {
    'specific_hours': [
                        {
                            "starts_at": "2015-12-2",
                            "ends_at": "2015-12-24",
                            "open_hours": [
                                "07:30-11:30",
                                "12:30-21:30"
                            ]
                        },
                        {
                            "starts_at": "2015-01-0",
                            "ends_at": "2015-01-03",
                            "open_hours": [
                                "07:30-11:30"
                            ]
                        }
                    ],
'subscription_products': {'presence_management': 
                            {'expiration_date': 1953291600,
                            'payment_type': {
                                    'free': 'iamfree',
                                    'test': "test",
                                    },
                            },
                        'review_management': 
                            {'expiration_date': 1511799660,
                            'payment_type': {
                                    'free': 'iamfree',
                                    'test': "test",
                                    },
                            'thiswillbedeleted': "deleteme",
                            }
                        },
}

beta = {
    'specific_hours': [
                        {
                            "starts_at": "2015-12-22",
                            "ends_at": "2015-12-24",
                            "open_hours": [
                                "07:30-11:30",
                                "12:30-21:30"
                            ]
                        },
                        {
                            "starts_at": "2015-01-0",
                            "ends_at": "2015-01-03",
                            "open_hours": [
                                "07:30-11:30"
                            ]
                        }
                    ],
'subscription_products': {'presence_management': 
                            {'expiration_date': 1953291600,
                            'payment_type': {
                                    'free': 'iamfree',
                                    'test': "test",
                                    },
                            },
                        'review_management': 
                            {'expiration_date': 1511799660,
                            'payment_type': {
                                    'free': 'iamfree',
                                    'test': "test",
                                    },
                            }
                        },
}

0 个答案:

没有答案