Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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_Dictionary - Fatal编程技术网

Python-->;断言字典存在于列表中

Python-->;断言字典存在于列表中,python,dictionary,Python,Dictionary,我有一本字典,我想检查一下它是否存在于列表中。 什么是最好的主张方式 tag_to_test_dict = { 'category': 'environment', 'name': 'quality assurance' },{ 'category': 'department', 'name': 'accounting' } gui_tags_list = [ 'department: accounting', 'environment:

我有一本字典,我想检查一下它是否存在于列表中。 什么是最好的主张方式

   tag_to_test_dict = {
    'category': 'environment',
    'name': 'quality assurance'
},{
    'category': 'department',
    'name': 'accounting'
}

gui_tags_list = [
    'department: accounting',
    'environment: quality assurance'
]
正如建议的那样,我尝试了这个方法,但没有得到很好的响应:

    if all(name in tag_to_test_dict for name in gui_tags_list):
        return 'True'
^修正了一点数据结构

要知道字典中的两个值都在列表中

使用
设置
对象的解决方案:

has_all = set(tag_to_test_dict.values()) <= set(v for d in gui_tags_list for k,v in d.items())
print(has_all)

发行集(其他)

设置因此,我实际解决问题的方法是:

for tag_to_test in tag_to_test_dict:
    assert '{}{} {}'.format(tag_to_test.values()[0], ":", tag_to_test.values()[1]) in gui_tags_list

你的比赛标准是什么?键还是值?我想知道字典中的两个值都在列表中,这对Python 2.7有效吗?不幸的是,我的项目是2.7project@new_to_coding,阅读OP的评论:我想知道字典中的两个值都在列表中。这会得到错误的输出,尽管我有点期待一个真实的回答。我做错了什么?你写道:知道字典中的两个值都在列表中。该方法返回应返回的
False
,因为
environment
不在DICT列表的值范围内。否则,你应该详细说明匹配的规则。我想我在某个地方犯了一个错误,我会挖掘一下,重新格式化这个问题。谢谢
for tag_to_test in tag_to_test_dict:
    assert '{}{} {}'.format(tag_to_test.values()[0], ":", tag_to_test.values()[1]) in gui_tags_list