Python判断两个变量是否相等,字典list不相等指出哪里不相等

import re

key1_prefix = "para1"
key2_prefix = "para2"


# 比较两个变量、list或者dict是否一样,若不一样,指出不一样的地方
def isEqual(para1, para2, key1_str=key1_prefix, key2_str=key2_prefix, ignore=[]):
    returnInfo = {"result": True, "info": "compare result is equal"}
    key1_temp = key1_str
    key2_temp = key2_str
    if isinstance(para1, dict) and isinstance(para2, dict):
        len1 = len(para1.keys())
        len2 = len(para2.keys())
        if len1 != len2:
            returnInfo["result"] = False
            returnInfo["info"] = f"{key1_str}'s len = {str(len1)} is not equal {key2_str}'s len = {str(len2)}"
            return returnInfo
        for key in para1:
            key1_str = f"{key1_temp}.{key}"
            if not keyMatchIgnore(key1_str, ignore):
                if key not in para2:
                    returnInfo["result"] = False
                    returnInfo["info"] = f"{key1_str} key not found in {key2_temp}'s keys [{str(para2.keys())}]"
                    return returnInfo
                else:
                    key2_str = f"{key2_temp}.{key}"
                    value1 = para1[key]
                    value2 = para2[key]
                    returnInfoSub = isEqual(value1, value2, key1_str, key2_str, ignore)
                    returnInfo["result"] = returnInfo["result"] and returnInfoSub["result"]
                    if returnInfoSub["result"] == False:
                        returnInfo["info"] = returnInfoSub["info"]
                        return returnInfo

    elif isinstance(para1, list) and isinstance(para2, list):
        len1 = len(para1)
        len2 = len(para2)
        if len1 != len2:
            returnInfo["result"] = False
            returnInfo["info"] = f"{key1_str}'s len = {str(len1)} is not equal {key2_str}'s len = {str(len2)}"
            return returnInfo
        for i in range(len1):
            key1_str = f"{key1_temp}[{i}]"
            key2_str = f"{key2_temp}[{i}]"
            if not keyMatchIgnore(key1_str, ignore):
                value1 = para1[i]
                value2 = para2[i]
                returnInfoSub = isEqual(value1, value2, key1_str, key2_str, ignore)
                returnInfo["result"] = returnInfo["result"] and returnInfoSub["result"]
                if returnInfoSub["result"] == False:
                    returnInfo["info"] = returnInfoSub["info"]
                    return returnInfo

    else:
        if not keyMatchIgnore(key1_str, ignore):
            returnInfo["result"] = (para1 == para2)
            if returnInfo["result"] == False:
                returnInfo["info"] = f"{key1_str}={para1} is not equal {key2_str}={para2}"
            return returnInfo

    return returnInfo


def getKey1WithoutPrefix(key1):
    key1_prefix_len = len(key1_prefix)
    return key1[key1_prefix_len:] if key1.startswith(key1_prefix + "[") else key1[key1_prefix_len + 1:]


def keyMatchIgnore(key, ignore):
    key = getKey1WithoutPrefix(key)
    for rule in ignore:
        rule = rule.replace(".", "\\.").replace("[", "\\[").replace("]", "\\]").replace("*", ".*")
        if re.match(rule, key) is not None:
            return True
    return False



if __name__ == "__main__":
    para1 = "1"
    para2 = "1"
    print(isEqual(para1, para2))

    para1 = 2
    para2 = "1"
    print(isEqual(para1, para2))

    para1 = 1
    para2 = 1
    print(isEqual(para1, para2))

    para1 = 2
    para2 = 1
    print(isEqual(para1, para2))

    para1 = [1, 2]
    para2 = [1, 2]
    print(isEqual(para1, para2))

    para1 = [1, 2, 3]
    para2 = [1, 2]
    print(isEqual(para1, para2))

    para1 = [1, 2, {"a": 2}]
    para2 = [1, 2, {"a": 2}]
    print(isEqual(para1, para2))

    para1 = [1, 2, {"a": 2}]
    para2 = [1, 2, {"a": 3}]
    print(isEqual(para1, para2))

    para1 = [1, 2, {"a": 2}]
    para2 = [1, 2, {"a": 2, "b": "fdd"}]
    print(isEqual(para1, para2))

    para1 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": "3"}, 2, "3"]}
    para2 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": "3"}, 2, "3"]}
    print(isEqual(para1, para2))

    para1 = {"a": {"b": [1, 23]},
             "d": [{"e": 2, "be": "3"}, 2, "3"]}
    para2 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": "3"}, 2, "3"]}
    print(isEqual(para1, para2))

    para1 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": 3}, 2, "3"]}
    para2 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": "3"}, 2, "3"]}
    print(isEqual(para1, para2))
    print(isEqual(para1, para2, ignore=["d[0].be"]))
    print(isEqual(para1, para2, ignore=["d[*].b*"]))

    para1 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be": 3}, 2, "3"]}
    para2 = {"a": {"c": 1,
                   "b": [1, 23]},
             "d": [{"e": 2, "be1": 3}, 2, "3"]}
    print(isEqual(para1, para2))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值