python字典的比较_python3 字典比较函数

#比较字典函数是否相等

def get_cmp_dict(self,src_data,dst_data):

if isinstance(src_data,str):

src_data=json.dumps(src_data)

if isinstance(dst_data,str):

dst_data=json.dumps(dst_data)

if len(src_data) != len(dst_data):

return False

else:

src_key=list(src_data.keys())

dst_key=list(dst_data.keys())

if operator.eq(src_key,dst_key):

src_val=list(src_data.values())

dst_val=list(dst_data.values())

if operator.eq(src_val,dst_val):

for key in src_data.keys():

if src_data[key] != dst_data[key]:

# print(src_data1[key])

return False

return True

else:

return False

else:

return False

#使用函数

dict1 = {'Name': 'asf', 'Age': 7}

dict2 = {'Name': 'fsafsad', 'Age': 27,"sdaf":23}

dict3 = {'Name': '33', 'Age': 27}

dict4 = {'Name': 'asf', 'Age': 7}

print(get_cmp_dict(dict1, dict3))

2.判断是否包含

#比较字典函数是否包含

def get_contain_dict(self,src_data,dst_data):

if isinstance(src_data,str):

src_data=json.dumps(src_data)

if isinstance(dst_data,str):

dst_data=json.dumps(dst_data)

else:

src_key=list(src_data.keys())

dst_key=list(dst_data.keys())

# print(str(src_key))

# print(str(dst_key))

pd=[False for c in src_key if c not in dst_key]

if pd:

return False

else:

src_val = list(src_data.values())

dst_val = list(dst_data.values())

pds = [False for c in src_val if c not in dst_val]

if pds:

return False

else:

return True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值