easydict的作用:可以使得以属性的方式去访问字典的值!

>>>from easydict import EasyDict as edict
>>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
>>> d.foo
3
>>> d.bar.x
1>>> d = edict(foo=3)
>>> d.foo
3

解析json目录时很有用

>>> from easydict import EasyDict as edict
>>> from simplejson import loads
>>> j = """{
"Buffer": 12,
"List1": [{"type" : "point", "coordinates" : [100.1,54.9] },{"type" : "point", "coordinates" : [109.4,65.1] },{"type" : "point", "coordinates" : [115.2,80.2] },{"type" : "point", "coordinates" : [150.9,97.8] }
]
}"""
>>> d = edict(loads(j))
>>> d.Buffer
12
>>> d.List1[0].coordinates[1]
54.9

也可以这样用

>>> d = EasyDict()
>>> d.foo = 3
>>> d.foo
3
>>> d = EasyDict(log=False)
>>> d.debug = True
>>> d.items()
[('debug', True), ('log', False)]>>> class Flower(EasyDict):
...     power = 1
...
>>> f = Flower({'height': 12})
>>> f.power
1
>>> f['power']
1

官方例子

Help on class EasyDict in module easydict:class EasyDict(__builtin__.dict)
Get attributes>>> d = EasyDict({'foo':3})
>>> d['foo']
3
>>> d.foo
3
>>> d.bar
Traceback (most recent call last):
...
AttributeError: 'EasyDict' object has no attribute 'bar'Works recursively>>> d = EasyDict({'foo':3, 'bar':{'x':1, 'y':2}})
>>> isinstance(d.bar, dict)
True
>>> d.bar.x
1Bullet-proof>>> EasyDict({})
{}
>>> EasyDict(d={})
{}
>>> EasyDict(None)
{}
>>> d = {'a': 1}
>>> EasyDict(**d)
{'a': 1}Set attributes>>> d = EasyDict()
>>> d.foo = 3
>>> d.foo
3
>>> d.bar = {'prop': 'value'}
>>> d.bar.prop
'value'
>>> d
{'foo': 3, 'bar': {'prop': 'value'}}
>>> d.bar.prop = 'newer'
>>> d.bar.prop
'newer'Values extraction>>> d = EasyDict({'foo':0, 'bar':[{'x':1, 'y':2}, {'x':3, 'y':4}]})
>>> isinstance(d.bar, list)
True
>>> from operator import attrgetter
>>> map(attrgetter('x'), d.bar)
[1, 3]
>>> map(attrgetter('y'), d.bar)
[2, 4]
>>> d = EasyDict()
>>> d.keys()
[]
>>> d = EasyDict(foo=3, bar=dict(x=1, y=2))
>>> d.foo
3
>>> d.bar.x
1Still like a dict though>>> o = EasyDict({'clean':True})
>>> o.items()
[('clean', True)]And like a class>>> class Flower(EasyDict):
...     power = 1
...
>>> f = Flower()
>>> f.power
1
>>> f = Flower({'height': 12})
>>> f.height
12
>>> f['power']
1
>>> sorted(f.keys())
['height', 'power']

转载:https://blog.csdn.net/m0_38082419/article/details/79079516

python字典操作 EasyDict()作用相关推荐

  1. python字典操作添加_Python字典常见操作实例小结【定义、添加、删除、遍历】

    本文实例总结了python字典常见操作.分享给大家供大家参考,具体如下: 简单的字典: 字典就是键值对key-value组合. #字典 键值对组合 alien_0 ={'color':'green', ...

  2. Python字典操作大全

    //2018.11.6 Python字典操作 1.对于python编程里面字典的定义有以下几种方法: >>> a = dict(one=1, two=2, three=3) > ...

  3. python 字典操作提取key,value

    http://blog.csdn.net/hhtnan/article/details/77164198 python 字典操作提取key,value dictionaryName[key] = va ...

  4. python字典操作题_python字典练习题

    python字典练习题 写代码:有如下字典按照要求实现每一个功能dict = {"k1":"v1","k2":"v2", ...

  5. python字典操作 遍历_Python字典遍历操作实例小结

    本文实例讲述了Python字典遍历操作.分享给大家供大家参考,具体如下: 1 遍历键值对 可以使用一个 for 循环以及方法 items() 来遍历这个字典的键值对. dict = {'evapora ...

  6. python字典调用_【python Dict】 python 字典操作

    python字典 是一个无序.以键值对存储的数据类型,数据关联性强.唯一一个映射数据类型.键:必须是可哈希(不可变的数据类型:字符串.数字.元组.bool)值,并且是唯一的 None: none 是一 ...

  7. python字典操作技巧_python的字典使用方法大全

    字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 . 键一般是唯一的,如 ...

  8. python 字典操作遍历

    # 字典即为键值对集合.字典可以有若干个键值对,如果键重复的以后一个为实际值. dict = {"name": "LCF", "age": ...

  9. python字典操作的方法_python 字典操作方法详解

    python 字典操作方法详解 一.总结 一句话总结: 字典就是键值对映射 ,像js和php中的键值对数组:{'name':'jamnes','age':'32'} 1.python字典的增删改查? ...

最新文章

  1. A Crowdsourcing Method for Correcting Sequencing Errors for the Third-generation Sequencing Data 一种用
  2. 震惊!EfficientDet终于开源了!
  3. STM32 C/C++ uCOSII 函数调用return 无法返回或者函数无法正常反回上一层函数的问题
  4. java计算混淆矩阵(分类指标:查准率P,查全率R,P和R的调和均值F1,正确率A)
  5. 大一计算机绩点3算什么水平,绩点只有3?我可以解释一下
  6. cnn生成图像显著图_基于CNN与图像前背景分离的显著目标检测
  7. 【计算机科学基础】图灵机原理概述
  8. 用计算机怎样弄出告白密码,数字表白密码 表白密码大全
  9. 思科/华为计算机网络工程师,软考网络工程师华为、思科指令大全
  10. 编译原理实验-PL0自底向上语法分析
  11. Digester简介和使用
  12. 数据库事务的四大特性,四种隔离级别,如何避免脏读、不可重复读、幻读(如何加锁)?
  13. 医药行业如何数字化转型 附医药行业数字化转型方案
  14. 晚安西南-----地破实验
  15. 集合竞价与连续竞价02
  16. python 相关性检验_Python中的相关分析correlation analysis的实现
  17. 【杂记】数据存储架构
  18. vue项目微信公众号title设置和调用接口动态修改
  19. 设计实现信用卡用户定时还款功能
  20. MFC WebBrowser获取网面完整源码 WebBrowser获取网面完整HTML

热门文章

  1. 利用ESP8266-12F实现与51单片机通信及温湿度传感器数据交互
  2. rsync+xinetd+inotify+sersync
  3. Spring源码构建项目,导入eclipse后,缺失spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.4.jar的解决办法
  4. 老男孩linux培训-python三期下载
  5. go语言比java高级在哪里
  6. 【学习笔记之Linux】工具之gcc/g++
  7. 新手java练习题100(1-5)
  8. 学习笔记-CCS-MSP430F5529[快速入门篇二]
  9. 渗透工具-Burpsuite
  10. 【Acwing提高】DP·背包