python判断字典包含另外一个字典

  1. 普通方法
# 预期结果
expected = {'username':'kaishui'}
# 实际结果
result={
    'code': 1 ,
    'username':'kaishui',
    'token':'ihbedvbwejhvkjvberkjvbkjgkesjvbbje'
}

def assert_dict(expected, result):
    for key in expected:
        if (key in result) & (result[key]==expected[key]):
            print('测试通过')
        else:
            raise Exception('断言不通过')

assert_dict(expected,result)
  1. set方法
# 预期结果
expected = {'username':'kaishui'}
# 实际结果
result={
    'code': 1 ,
    'username':'kaishui',
    'token':'ihbedvbwejhvkjvberkjvbkjgkesjvbbje'
}

expected_set = set(expected.items())
result_set = set(result.items())

print(expected_set.issubset(result_set))

posted @ 2021-05-10 20:06  该显示昵称已被使用了  阅读(362)  评论(0编辑  收藏  举报