资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Python原生字典的示例分析

这篇文章主要介绍Python原生字典的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网站空间、营销软件、网站建设、贡觉网站维护、网站推广。

字典是 Python 中基础的数据结构之一,字典的使用,可以说是非常的简单粗暴,但即便是这样一个与世无争的数据结构,仍然有很多人 "看不惯它" 。

Python原生字典的示例分析

也许你并不觉得,但我相信,你看了这篇文章后,一定会和我一样,对原生字典开始有了偏见。我举个简单的例子吧当你想访问字典中的某个 key  时,你需要使用字典特定的访问方式,而这种方式需要你键入 一对中括号 还有 一对引号

>>> profile = dict(name="iswbm") >>> profile {'name': 'iswbm'} >>> profile["name"] 'iswbm'

是不是开始觉得忍无可忍了?如果可以像调用对象属性一样使用 . 去访问 key 就好了,可以省去很多多余的键盘击入,就像这样子

>>> profile.name 'iswbm'

是的,今天这篇文章就是跟大家分享一种可以直接使用 . 访问和操作字典的一个黑魔法库 -- munch。

1. 安装方法

使用如下命令进行安装

$ python -m pip install munch

2. 简单示例

munch 有一个 Munch 类,它继承自原生字典,使用 isinstance 可以验证

>>> from munch import Munch >>> profile = Munch() >>> isinstance(profile, dict) True >>>

并实现了点式赋值与访问,profile.name 与 profile['name'] 是等价的

>>> profile.name = "iswbm" >>> profile.age = 18 >>> profile Munch({'name': 'iswbm', 'age': 18}) >>> >>> profile.name 'iswbm' >>> profile["name"] 'iswbm'

3. 兼容字典的所有操作

本身 Munch 继承自 dict,dict 的操作也同样适用于 Munch 对象,不妨再来验证下首先是:增删改查

# 新增元素 >>> profile["gender"] = "male" >>> profile Munch({'name': 'iswbm', 'age': 18, 'gender': 'male'})  # 修改元素 >>> profile["gender"] = "female" >>> profile Munch({'name': 'iswbm', 'age': 18, 'gender': 'female'})  # 删除元素 >>> profile.pop("gender") 'female' >>> profile Munch({'name': 'iswbm', 'age': 18}) >>> >>> del profile["age"] >>> profile Munch({'name': 'iswbm'})

再者是:一些常用方法

>>> profile.keys() dict_keys(['name']) >>> >>> profile.values() dict_values(['iswbm']) >>> >>> profile.get('name') 'iswbm' >>> profile.setdefault('gender', 'male') 'male' >>> profile Munch({'name': 'iswbm', 'gender': 'male'})

4. 设置返回默认值

当访问一个字典中不存在的 key 时,会报 KeyError 的错误

>>> profile = {} >>> profile["name"] Traceback (most recent call last):   File "", line 1, in  KeyError: 'name'

对于这种情况,通常我们会使用 get 来规避

>>> profile = {} >>> profile.get("name", "undefined") 'undefined'

当然你在 munch 中仍然可以这么用,不过还有一种更好的方法:使用 DefaultMunch,它会在你访问不存在的 key  时,给你返回一个设定好的默认值

>>> from munch import DefaultMunch >>> profile = DefaultMunch("undefined", {"name": "iswbm"}) >>> profile DefaultMunch('undefined', {'name': 'iswbm'}) >>> profile.age 'undefined' >>> profile DefaultMunch('undefined', {'name': 'iswbm'})

5. 工厂函数自动创建key

上面使用 DefaultMunch 仅当你访问不存在的 key 是返回一个默认值,但这个行为并不会修改原 munch  对象的任何内容。若你想访问不存在的 key 时,自动触发给原 munch 中新增你想要访问的 key ,并为其设置一个默认值,可以试一下  DefaultFactoryMunch 传入一个工厂函数。

>>> from munch import DefaultFactoryMunch >>> profile = DefaultFactoryMunch(list, name='iswbm') >>> profile DefaultFactoryMunch(list, {'name': 'iswbm'}) >>> >>> profile.brothers [] >>> profile DefaultFactoryMunch(list, {'name': 'iswbm', 'brothers': []})

6. 序列化的支持

Munch 支持序列化为 JSON 或者 YAML 格式的字符串对象

转换成 JSON:

>>> from munch import Munch >>> munch_obj = Munch(foo=Munch(lol=True), bar=100, msg='hello') >>> >>> import json >>> json.dumps(munch_obj) '{"foo": {"lol": true}, "bar": 100, "msg": "hello"}'

转换成 YAML:

>>> from munch import Munch >>> munch_obj = Munch(foo=Munch(lol=True), bar=100, msg='hello') >>> import yaml >>> yaml.dump(munch_obj) '!munch.Munch\nbar: 100\nfoo: !munch.Munch\n  lol: true\nmsg: hello\n' >>> >>> print(yaml.dump(munch_obj)) !munch.Munch bar: 100 foo: !munch.Munch   lol: true msg: hello  >>>

建议使用 safe_dump 去掉 !munch.Munch:

>>> print(yaml.safe_dump(munch_obj)) bar: 100 foo:   lol: true msg: hello

以上是“Python原生字典的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


网站名称:Python原生字典的示例分析
当前链接:http://cdkjz.cn/article/psegsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220