我有两个字典,想遍历查询dic的值是否在dic1中,如果不在返回这部分不在的值
dic={'0': 'rm-pz5l62z7h7p7v4ocj', '1': 'rm-pz5irvp287ha32ecu', '2': 'rm-pz5049z6gvl6hoo7d', '3': 'rm-pz5049z6gvl6hoo9d'}
dic1={1: ('a', 'rm-pz5l62z7h7p7v4ocj', 46048, 'Physical'), 2: ('a', 'rm-pz5irvp287ha32ecu', 3824, 'Physical'), 3: ('a', 'rm-pz5049z6gvl6hoo7d', 11632, 'Physical') }
最后返回应该是这个
dic2={'rm-pz5049z6gvl6hoo9d'}
用for循环遍历字典dic和字典dic1,然后不断比对其值就可以了,最后是要将结果保存到列表dic2吗,代码如下:
参考链接:
遍历字典_遍历字典_桑夷的博客-CSDN博客
Python 列表
dic={'0': 'rm-pz5l62z7h7p7v4ocj', '1': 'rm-pz5irvp287ha32ecu', '2': 'rm-pz5049z6gvl6hoo7d', '3': 'rm-pz5049z6gvl6hoo9d'}
dic1={1: ('a', 'rm-pz5l62z7h7p7v4ocj', 46048, 'Physical'), 2: ('a', 'rm-pz5irvp287ha32ecu', 3824, 'Physical'), 3: ('a', 'rm-pz5049z6gvl6hoo7d', 11632, 'Physical') }
dic2=[]
#https://blog.csdn.net/weixin_32528123/article/details/112714130
find = 0 #寻找标志,用于表示dic的值是否存在于字典dic1的值列表中
#遍历字典disc的每个值,将它与disc1的值的列表中的每一个都比较,如果不存在于dic1的值中,则打印dic这个值
for v in dic.values():
for v1 in dic1.values(): #遍历disc1的每一个值
for v1Obj in v1: #遍历disc1每一个值列表中的每一项
if(str(v1Obj)) == v: #如果存在,则置标记find为1,表示找到,然后退出此循环
find=1
break;
if find==0: #如果没有找到,则打印字典dic在这个值
#print(v," ")
dic2.append(v)
find=0 #寻找标记置0,表示默认字典disc的当前值不存在于字典dic1的值列表中
print("dic2=",dic2)