检查列表值在 python 的字典中

check list value is in dictionary for python

如何检查列表中的所有值是否都在字典中?

'''

dict={
    "x": {
        "y": [
                {
                    "Id": "AB",
                    "Name": "C D E",
                    "cat": [
                        "cat"
                            ],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                },
                {
                    "Id": "CD",
                    "sourceName": "FGE",
                    "cat": [],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                }
            ]
        "z":[   {
                    "Id": "YC",
                    "Name": "C D E",
                    "cat": [
                        "cat"
                            ],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                },
                {
                    "Id": "PO",
                    "sourceName": "FGE",
                    "cat": [],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                }
            ]
        }      
    }
    
    list=['AB','CD']
    
    this is what I did but its not working
    
    def comp():
        leng=len(dict['x']['y'])
        for abc in leng:
            if dict['x']['y'][abc]['id'] == list[0]:
                print(list)
                list.pop(0)
                if list == []:
                    result='OK'
                    break
                else: result='Not OK!'

'''

我想创建一个函数来比较列表中的所有值是否存在于字典“y”中['Id']

如果所有都存在那么我想得到结果= ok 否则结果= not ok 感谢您的帮助

您的字典缺少逗号,因此会引发错误:

dict={
    "x": {
        "y": [
                {
                    "Id": "AB",
                    "Name": "C D E",
                    "cat": [
                        "cat"
                            ],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                },
                {
                    "Id": "CD",
                    "sourceName": "FGE",
                    "cat": [],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                }
            ],
        "z":[   {
                    "Id": "YC",
                    "Name": "C D E",
                    "cat": [
                        "cat"
                            ],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                },
                {
                    "Id": "PO",
                    "sourceName": "FGE",
                    "cat": [],
                    "stat": "NO",
                    "count1": 0,
                    "count2": 0,
                    "results": []
                }
            ]
        }      
    }

您应该使用值列表获取字典中的键,而不是:

for abc in dict

应该是:

for abc in dict['x']['y]

要检查 dict_value 是否在列表中,您应该这样做:

if abc['Id'] in lst:
   lst.remove(abc['Id'])

总的来说,它应该是这样的:

def comp():
    for abc in dict['x']['y']:
        if abc['Id'] in lst:
            lst.remove(abc['Id'])
    if not lst:
        return "ok"
    return "not ok"