• Python--EasyDict()


    写在前面:当遇到一个陌生的python第三方库时,可以去pypi这个主页查看描述以迅速入门!
    或者
    1. import time
    2. dir(time)


    easydict的作用:可以使得以属性的方式去访问字典的值!
    1. >>> from easydict import EasyDict as edict
    2. >>> d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
    3. >>> d.foo
    4. 3
    5. >>> d.bar.x
    6. 1
    7. >>> d = edict(foo=3)
    8. >>> d.foo
    9. 3
    解析json目录时很有用
    1. >>> from easydict import EasyDict as edict
    2. >>> from simplejson import loads
    3. >>> j = """{
    4. "Buffer": 12,
    5. "List1": [
    6. {"type" : "point", "coordinates" : [100.1,54.9] },
    7. {"type" : "point", "coordinates" : [109.4,65.1] },
    8. {"type" : "point", "coordinates" : [115.2,80.2] },
    9. {"type" : "point", "coordinates" : [150.9,97.8] }
    10. ]
    11. }"""
    12. >>> d = edict(loads(j))
    13. >>> d.Buffer
    14. 12
    15. >>> d.List1[0].coordinates[1]
    16. 54.9
    也可以这样用
    1. >>> d = EasyDict()
    2. >>> d.foo = 3
    3. >>> d.foo
    4. 3
    1. >>> d = EasyDict(log=False)
    2. >>> d.debug = True
    3. >>> d.items()
    4. [('debug', True), ('log', False)]

    1. >>> class Flower(EasyDict):
    2. ... power = 1
    3. ...
    4. >>> f = Flower({'height': 12})
    5. >>> f.power
    6. 1
    7. >>> f['power']
    8. 1







  • 相关阅读:
    购物网站被p.egou.com强制恶意劫持
    css下拉菜单
    StringToInt
    JframeMaxSize
    frameMaxSize
    inputChar
    英语要求
    sciAndSubject
    fileRename
    tensorflowOnWindows
  • 原文地址:https://www.cnblogs.com/leebxo/p/11735517.html
Copyright © 2020-2023  润新知