import os, sysimport re #1.列出不同的键值 reload(sys) sys.setdefaultencoding("utf8") dct0 = {"name":"zhang","age":"23"} dct1 = {"name":"san","age":"23"} def cmpdicts(dct0, dct1): diffs = set() keys = set(dct0.keys() + dct1.keys()) for k in keys: if cmp(dct0.get(k), dct1.get(k)): diffs.add(k) return diffs dict_diff = cmpdicts(dct0, dct1) print "字典不同的值:" print dict_diff
import os, sys import re #列出所有的不同键值 reload(sys) sys.setdefaultencoding("utf8") dct0 = {"name":"zhang","age":"23"} dct1 = {"name":"san","age":"23","sex":"1"} print "字典dct0的值:" + str(dct0) print "字典dct1的值:" + str(dct1) differ = set(dct0.items()) ^ set(dct1.items()) print "字典不同的值:" print(differ) print "查看字典dct0和字典dct1共有的key" print set(dct0.keys()) & set(dct1.keys()) print "查看字典dct0和字典dct1不共有的key" print set(dct0.keys()) ^ set(dct1.keys()) print "查看在字典dct1里面而不在字典dct0里面的key:" print set(dct1.keys()) - set(dct0.keys()) print "查看字典dct0和字典dct1相同的键值对:" print set(dct0.items()) & set(dct1.items())
标签:set,python,keys,键值,print,字典,dct1,dct0 From: https://www.cnblogs.com/buchi-baicai/p/17466550.html