小编tho*_*gnv的帖子

检查列表中是否存在字典

我有一个包含一些dicts的列表:

dict1 = {
'key1': 'value1',
'key2': 'value2',
}

dict2 = {
'key1': 'value3',
'key2': 'value4',
}

list = [dict1, dict2]
Run Code Online (Sandbox Code Playgroud)

我用它来检查列表中是否存在dict,例如我改为dict1

dict1 = {
'key1': 'something',
'key2': 'value2',
}
Run Code Online (Sandbox Code Playgroud)

现在,检查 dict1

if dict1 in list:
    print('Exists')
else:
    print('Not exists')
Run Code Online (Sandbox Code Playgroud)

它必须返回'Not exists',但事实并非如此.

python dictionary list python-3.x

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

dictionary ×1

list ×1

python ×1

python-3.x ×1