Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对照Python字典中的键值检查列表中的值_Python_List_Dictionary_Compare_Key Value - Fatal编程技术网

对照Python字典中的键值检查列表中的值

对照Python字典中的键值检查列表中的值,python,list,dictionary,compare,key-value,Python,List,Dictionary,Compare,Key Value,我有两个数据源,一个是列表,另一个是字典列表。 我的数据如下所示: need_placeholder = ['1200', '1300', '1400'] ad_dict = [{"Name": "A", "ID": "1999"}, {"Name": "B", "ID": "1299"}, {"Name": "C", "ID": "1400"}] 我需要检查need\u占位符项是否等于ad\u dict中的ID值。这是我的剧本: for item in need_pl

我有两个数据源,一个是列表,另一个是字典列表。 我的数据如下所示:

need_placeholder = ['1200', '1300', '1400']
ad_dict = [{"Name": "A", "ID": "1999"}, {"Name": "B", "ID": "1299"}, 
           {"Name": "C", "ID": "1400"}]
我需要检查
need\u占位符
项是否等于
ad\u dict
中的
ID
值。这是我的剧本:

for item in need_placeholder:
    adpoint_key = item 
    for index, my_dict in enumerate(ad_dict):
        if my_dict["ID"] == adpoint_key:
            continue
        else:    
            print(f'No key exists for {adpoint_key}')
输出为:

我期望的输出是:


我如何在不遍历字典或列表的情况下比较这些值

您的
else
在循环中的位置不正确。对于每个输出,该内部循环运行多次。如果首先将
ID
值拉入一个集合,则可以完全避免该循环:

need_placeholder = ['1200', '1300', '1400']
ad_dict = [{"Name": "A", "ID": "1999"}, {"Name": "B", "ID": "1299"}, {"Name": "C", "ID": "1400"}]

ad_values = set(d['ID'] for d in ad_dict)

for v in need_placeholder:
    if v not in ad_values:
        print(f'no key exits for {v}')
印刷品:

no key exits for 1200
no key exits for 1300
如果顺序不重要,您可以将整个过程作为一组操作来完成:

for v in set(need_placeholder) - ad_values:
    print(f'no key exits for {v}')
你可以试试这个

>>> need_placeholder = ['1200', '1300', '1400']
>>> ad_dict = [{"Name": "A", "ID": "1999"}, {"Name": "B", "ID": "1299"}, 
               {"Name": "C", "ID": "1400"}]
>>> keys={d['ID'] for d in ad_dict} # Set of all unique ID values
>>> [key for key in need_placeholder if not key in keys]
# ['1200', '1300']
你可以用

如果你不在乎秩序

set(need_placeholder)-keys
# {'1300', '1200'}
使用

使用


一种快速的方法是使用嵌套列表理解

[print('No key for {}'.format(x)) for x in need_placeholder if x not in [y['ID'] for y in ad_dict]]

我建议使用一个类,而不是列表中的那么多字典。我必须保持当前的格式。
list(filterfalse(lambda x:x in keys, need_placeholder))
# ['1200', '1300']
set(need_placeholder)-keys
# {'1300', '1200'}
>>> for key in need_placeholder:
...     if all(key != d['ID'] for d in ad_dict):
...         print(f'No key exists for {key}')
...
No key exists for 1200
No key exists for 1300
>>> for key in need_placeholder:
...     for d in ad_dict:
...             if key==d['ID']:
...                     break
...     else:
...             print(f'No key {key}')
...
No key 1200
No key 1300
[print('No key for {}'.format(x)) for x in need_placeholder if x not in [y['ID'] for y in ad_dict]]
allowed_values = {dic["ID"] for dic in ad_dict}

for item in need_placeholder:
    if item not in allowed_values:
        print(f'No key exists for {item}')
for item in need_placeholder:
    adpoint_key = item
    duplicate_exist = False # flag that will be update if key exists
    for index, my_dict in enumerate(ad_dict):
        if my_dict["ID"] == adpoint_key:
            duplicate_exist = True    # flag is updated as key exists
    not duplicate_exist and print(f'No key exists for {adpoint_key}') # for all non duplicate print function is invoked at the end of inner loop