偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

終結(jié) Python 原生字典?這個(gè)庫要逆天改命了

開發(fā) 后端
字典是 Python 中基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)之一,字典的使用,可以說是非常的簡單粗暴,但即便是這樣一個(gè)與世無爭的數(shù)據(jù)結(jié)構(gòu),仍然有很多人 "看不慣它" 。

大家好,我是明哥,今天來聊一聊與字典相關(guān)的話題。

字典是 Python 中基礎(chǔ)的數(shù)據(jù)結(jié)構(gòu)之一,字典的使用,可以說是非常的簡單粗暴,但即便是這樣一個(gè)與世無爭的數(shù)據(jù)結(jié)構(gòu),仍然有很多人 "看不慣它" 。

[[401175]]

也許你并不覺得,但我相信,你看了這篇文章后,一定會(huì)和我一樣,對原生字典開始有了偏見。我舉個(gè)簡單的例子吧當(dāng)你想訪問字典中的某個(gè) key 時(shí),你需要使用字典特定的訪問方式,而這種方式需要你鍵入 一對中括號 還有 一對引號

  1. >>> profile = dict(name="iswbm"
  2. >>> profile 
  3. {'name': 'iswbm'} 
  4. >>> profile["name"] 
  5. 'iswbm' 

是不是開始覺得忍無可忍了?如果可以像調(diào)用對象屬性一樣使用 . 去訪問 key 就好了,可以省去很多多余的鍵盤擊入,就像這樣子

  1. >>> profile.name 
  2. 'iswbm' 

是的,今天這篇文章就是跟大家分享一種可以直接使用 . 訪問和操作字典的一個(gè)黑魔法庫 -- munch。

1. 安裝方法

使用如下命令進(jìn)行安裝

  1. $ python -m pip install munch 

2. 簡單示例

munch 有一個(gè) Munch 類,它繼承自原生字典,使用 isinstance 可以驗(yàn)證

  1. >>> from munch import Munch 
  2. >>> profile = Munch() 
  3. >>> isinstance(profile, dict) 
  4. True 
  5. >>> 

并實(shí)現(xiàn)了點(diǎn)式賦值與訪問,profile.name 與 profile['name'] 是等價(jià)的

  1. >>> profile.name = "iswbm" 
  2. >>> profile.age = 18 
  3. >>> profile 
  4. Munch({'name': 'iswbm', 'age': 18}) 
  5. >>> 
  6. >>> profile.name 
  7. 'iswbm' 
  8. >>> profile["name"] 
  9. 'iswbm' 

3. 兼容字典的所有操作

本身 Munch 繼承自 dict,dict 的操作也同樣適用于 Munch 對象,不妨再來驗(yàn)證下首先是:增刪改查

  1. # 新增元素 
  2. >>> profile["gender"] = "male" 
  3. >>> profile 
  4. Munch({'name': 'iswbm', 'age': 18, 'gender': 'male'}) 
  5.  
  6. # 修改元素 
  7. >>> profile["gender"] = "female" 
  8. >>> profile 
  9. Munch({'name': 'iswbm', 'age': 18, 'gender': 'female'}) 
  10.  
  11. # 刪除元素 
  12. >>> profile.pop("gender") 
  13. 'female' 
  14. >>> profile 
  15. Munch({'name': 'iswbm', 'age': 18}) 
  16. >>> 
  17. >>> del profile["age"] 
  18. >>> profile 
  19. Munch({'name': 'iswbm'}) 

再者是:一些常用方法

  1. >>> profile.keys() 
  2. dict_keys(['name']) 
  3. >>> 
  4. >>> profile.values() 
  5. dict_values(['iswbm']) 
  6. >>> 
  7. >>> profile.get('name') 
  8. 'iswbm' 
  9. >>> profile.setdefault('gender', 'male') 
  10. 'male' 
  11. >>> profile 
  12. Munch({'name': 'iswbm', 'gender': 'male'}) 

4. 設(shè)置返回默認(rèn)值

當(dāng)訪問一個(gè)字典中不存在的 key 時(shí),會(huì)報(bào) KeyError 的錯(cuò)誤

  1. >>> profile = {} 
  2. >>> profile["name"] 
  3. Traceback (most recent call last): 
  4.   File "<stdin>", line 1, in <module> 
  5. KeyError: 'name' 

對于這種情況,通常我們會(huì)使用 get 來規(guī)避

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

當(dāng)然你在 munch 中仍然可以這么用,不過還有一種更好的方法:使用 DefaultMunch,它會(huì)在你訪問不存在的 key 時(shí),給你返回一個(gè)設(shè)定好的默認(rèn)值

  1. >>> from munch import DefaultMunch 
  2. >>> profile = DefaultMunch("undefined", {"name": "iswbm"}) 
  3. >>> profile 
  4. DefaultMunch('undefined', {'name': 'iswbm'}) 
  5. >>> profile.age 
  6. 'undefined' 
  7. >>> profile 
  8. DefaultMunch('undefined', {'name': 'iswbm'}) 

5. 工廠函數(shù)自動(dòng)創(chuàng)建key

上面使用 DefaultMunch 僅當(dāng)你訪問不存在的 key 是返回一個(gè)默認(rèn)值,但這個(gè)行為并不會(huì)修改原 munch 對象的任何內(nèi)容。若你想訪問不存在的 key 時(shí),自動(dòng)觸發(fā)給原 munch 中新增你想要訪問的 key ,并為其設(shè)置一個(gè)默認(rèn)值,可以試一下 DefaultFactoryMunch 傳入一個(gè)工廠函數(shù)。

  1. >>> from munch import DefaultFactoryMunch 
  2. >>> profile = DefaultFactoryMunch(list, name='iswbm'
  3. >>> profile 
  4. DefaultFactoryMunch(list, {'name': 'iswbm'}) 
  5. >>> 
  6. >>> profile.brothers 
  7. [] 
  8. >>> profile 
  9. DefaultFactoryMunch(list, {'name': 'iswbm', 'brothers': []}) 

6. 序列化的支持

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

轉(zhuǎn)換成 JSON:

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

轉(zhuǎn)換成 YAML:

  1. >>> from munch import Munch 
  2. >>> munch_obj = Munch(foo=Munch(lol=True), bar=100msg='hello'
  3. >>> import yaml 
  4. >>> yaml.dump(munch_obj) 
  5. '!munch.Munch\nbar: 100\nfoo: !munch.Munch\n  lol: true\nmsg: hello\n' 
  6. >>> 
  7. >>> print(yaml.dump(munch_obj)) 
  8. !munch.Munch 
  9. bar: 100 
  10. foo: !munch.Munch 
  11.   lol: true 
  12. msg: hello 
  13.  
  14. >>> 

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

  1. >>> print(yaml.safe_dump(munch_obj)) 
  2. bar: 100 
  3. foo: 
  4.   lol: true 
  5. msg: hello 

以上就是關(guān)于 munch 的使用全解,替換原生字典絕無問題,munch 的進(jìn)一步封裝使得數(shù)據(jù)的訪問及操作更得更加 Pythonic 了,希望有一天這個(gè)特性能夠體現(xiàn)在原生的字典上。

 

責(zé)任編輯:趙寧寧 來源: Python編程時(shí)光
相關(guān)推薦

2018-05-04 10:45:58

戴爾

2025-03-31 00:00:01

2019-03-04 08:48:23

Spring WebFJavaIO

2022-08-31 15:57:11

程序員

2014-09-02 17:33:05

魅族黃章MX4

2021-07-06 07:21:16

Spring 安全平臺

2023-09-26 07:22:20

2022-05-17 08:40:20

PythonWiFi密碼代碼

2017-05-09 16:20:47

3D打印3D食品

2018-01-16 08:40:13

SSD市場缺貨

2017-02-17 16:43:15

人工智能AI技術(shù)Wear 2.0

2022-04-15 15:11:41

清華計(jì)算機(jī)研究所

2018-05-23 15:01:46

程序員WiFi生產(chǎn)商

2025-03-04 00:13:10

2022-04-29 10:27:58

數(shù)據(jù)庫刪庫MySQL

2013-01-09 10:02:06

U盤金士頓1TB容量

2019-10-31 15:13:11

Python

2012-02-22 09:16:31

JavaJava SE 6

2019-05-15 10:55:07

機(jī)器學(xué)習(xí)數(shù)據(jù)庫索引

2016-10-08 10:09:16

華為HDG開發(fā)者
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號