python嵌套字典比较值,取值的实例详解


本文摘自php中文网,作者零下一度,侵删。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#取值

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')

  结果:

比较大小

1

2

3

4

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嵌套字典比较值,取值的实例详解的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python是用什么写的

Python输出怎么取消空格

Python为什么要用class

怎么用Python画花朵

使用Python进行手机号和数字的随机生成方法

Python线程池threadpool使用篇

Python变量赋值的步奏详解

使用Python时多少有人走过的坑!避险!

安装Python时没有scripts文件夹如何处理

Python面向对象编程中类和实例的简单讲解(附示例)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...