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))
Python判断两个变量是否相等,字典list不相等指出哪里不相等
最新推荐文章于 2024-08-06 17:00:20 发布