#取值 import types allGuests = {'Alice': {'apples': 5, 'pretzels': {'12':{'beijing':456}}}, 'Bob': {'ham sandwiches': 3, 'apple': 2}, 'Carol': {'cups': 3, 'apple pies': 1}} def dictget(dict1,obj,default=None): for k,v in dict1.items(): if k == obj: print(v) else: if type(v) is dict: re=dictget(v,obj) if re is not default: print(re) dictget(allGuests,'beijing')
结果:
比较大小
def bijiaodict(dict1,dict2):for k,v in dict1.items():for k2,v2 in dict2.items():if k==k2 and v==v2:print('dict1=dict2')else:print('dict1!=dict2') dict1={'2':'6'} dict2={2:{1:{1:8}}} bijiaodict(dict1,dict2)
结果:
以上就是python嵌套字典比较值,取值的实例详解的详细内容,更多请关注php中文网其它相关文章!
已抢186309个
抢已抢67172个
抢已抢352816个
抢已抢666870个
抢已抢121768个
抢已抢22702个
抢