【问题标题】:Comparing dictionary of list of dictionary/nested dictionary比较字典列表/嵌套字典的字典
【发布时间】:2022-11-22 19:37:49
【问题描述】:

有两个 dict main 和 input,我想验证“输入”,以便字典和嵌套字典列表中的所有键(如果存在/所有键都是可选的)匹配 main 的键,如果不是错误/不同的键应该作为输出返回

main = "app":[{
    "name": str,
    "info": [
        {
        "role": str,
        "scope": {"groups": list}
        }
        ]
    },{
    "name": str,
    "info": [
        {"role": str}
            ]
}]

input = "app":[{
    'name': 'nms',
    'info': [
        {
        'role': 'user',
        'scope': {'groups': ['xyz']
                }
        }]
},{
    'name': 'abc', 
    'info': [
    {'rol': 'user'}
    ]
}]

当将输入与 main 进行比较时,错误/不同的键应该作为输出给出,在这种情况下

['rol']

【问题讨论】:

    标签: python list dictionary recursion comparison


    【解决方案1】:

    schema 模块正是这样做的。 您可以捕获 SchemaUnexpectedTypeError 以查看哪些数据与您的模式不匹配。

    另外,请确保不要使用 input 作为变量名,因为它是内置函数的名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      相关资源
      最近更新 更多