終結(jié) Python 原生字典?這個(gè)庫(kù)要逆天改命了
↑↑↑關(guān)注后"星標(biāo)"簡(jiǎn)說(shuō)Python
人人都可以簡(jiǎn)單入門(mén)Python、爬蟲(chóng)、數(shù)據(jù)分析 簡(jiǎn)說(shuō)Python推薦 來(lái)源:Python編程時(shí)光 作者: 寫(xiě)代碼的明哥

>>> profile = dict(name="iswbm")
>>> profile
{'name': 'iswbm'}
>>> profile["name"]
'iswbm'
. 去訪問(wèn) key 就好了,可以省去很多多余的鍵盤(pán)擊入,就像這樣子>>> profile.name
'iswbm'
. 訪問(wèn)和操作字典的一個(gè)黑魔法庫(kù) -- munch。1. 安裝方法
$ python -m pip install munch
2. 簡(jiǎn)單示例
>>> from munch import Munch
>>> profile = Munch()
>>> isinstance(profile, dict)
True
>>>
profile.name 與 profile['name'] 是等價(jià)的>>> profile.name = "iswbm"
>>> profile.age = 18
>>> profile
Munch({'name': 'iswbm', 'age': 18})
>>>
>>> profile.name
'iswbm'
>>> profile["name"]
'iswbm'
3. 兼容字典的所有操作
# 新增元素
>>> 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. 設(shè)置返回默認(rèn)值
>>> profile = {}
>>> profile["name"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'name'
>>> profile = {}
>>> profile.get("name", "undefined")
'undefined'
>>> from munch import DefaultMunch
>>> profile = DefaultMunch("undefined", {"name": "iswbm"})
>>> profile
DefaultMunch('undefined', {'name': 'iswbm'})
>>> profile.age
'undefined'
>>> profile
DefaultMunch('undefined', {'name': 'iswbm'})
5. 工廠函數(shù)自動(dòng)創(chuàng)建key
DefaultMunch 僅當(dāng)你訪問(wèn)不存在的 key 是返回一個(gè)默認(rèn)值,但這個(gè)行為并不會(huì)修改原 munch 對(duì)象的任何內(nèi)容。DefaultFactoryMunch 傳入一個(gè)工廠函數(shù)。>>> from munch import DefaultFactoryMunch
>>> profile = DefaultFactoryMunch(list, name='iswbm')
>>> profile
DefaultFactoryMunch(list, {'name': 'iswbm'})
>>>
>>> profile.brothers
[]
>>> profile
DefaultFactoryMunch(list, {'name': 'iswbm', 'brothers': []})
6. 序列化的支持
>>> 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"}'
>>> 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
--END--
掃碼即可加我微信
觀看朋友圈,獲取最新學(xué)習(xí)資源
學(xué)習(xí)更多: 整理了我開(kāi)始分享學(xué)習(xí)筆記到現(xiàn)在超過(guò)250篇優(yōu)質(zhì)文章,涵蓋數(shù)據(jù)分析、爬蟲(chóng)、機(jī)器學(xué)習(xí)等方面,別再說(shuō)不知道該從哪開(kāi)始,實(shí)戰(zhàn)哪里找了
優(yōu)秀的讀者都知道,“點(diǎn)贊”傳統(tǒng)美德不能丟 
評(píng)論
圖片
表情
