11個好用到起飛的「Python字典」知識點!
? 人生苦短,快學(xué)Python!
字典這個概念,將會伴隨著你后面的學(xué)習(xí)和工作。字典是否是無序的
>>>?#?Python?2.7
>>>?a_dict?=?{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
>>>?a_dict
{'color':?'blue',?'pet':?'dog',?'fruit':?'apple'}
>>>?a_dict
{'color':?'blue',?'pet':?'dog',?'fruit':?'apple'}
>>>?#?Python?3.5
>>>?a_dict?=?{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
>>>?a_dict
{'color':?'blue',?'pet':?'dog',?'fruit':?'apple'}
>>>?a_dict
{'color':?'blue',?'pet':?'dog',?'fruit':?'apple'}
>>>?a_dict?=?{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
>>>?a_dict
{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
>>>?a_dict
{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
鍵值互換
>>>?a_dict?=?{'one':?1,?'two':?2,?'thee':?3,?'four':?4}
>>>?new_dict?=?{}
>>>?for?key,?value?in?a_dict.items():
...?????new_dict[value]?=?key
...
>>>?new_dict
{1:?'one',?2:?'two',?3:?'thee',?4:?'four'}
依據(jù)某種條件,過濾字典
>>>?a_dict?=?{'one':?1,?'two':?2,?'thee':?3,?'four':?4}
>>>?new_dict?=?{}??#?Create?a?new?empty?dictionary
>>>?for?key,?value?in?a_dict.items():
...?????if?value?<=?2:
...?????????new_dict[key]?=?value
...
>>>?new_dict
{'one':?1,?'two':?2}
利用字典中的值,做一些計算
>>>?incomes?=?{'apple':?5600.00,?'orange':?3500.00,?'banana':?5000.00}
>>>?total_income?=?0.00
>>>?for?value?in?incomes.values():
...?????total_income?+=?value??#?Accumulate?the?values?in?total_income
...
>>>?total_income
14100.0
字典推導(dǎo)式
>>>?objects?=?['blue',?'apple',?'dog']
>>>?categories?=?['color',?'fruit',?'pet']
>>>?a_dict?=?{key:?value?for?key,?value?in?zip(categories,?objects)}
>>>?a_dict
{'color':?'blue',?'fruit':?'apple',?'pet':?'dog'}
利用字典推導(dǎo)式,實現(xiàn)鍵值轉(zhuǎn)換
>>>?a_dict?=?{'one':?1,?'two':?2,?'thee':?3,?'four':?4}
>>>?new_dict?=?{value:?key?for?key,?value?in?a_dict.items()}
>>>?new_dict
{1:?'one',?2:?'two',?3:?'thee',?4:?'four'}
利用字典推導(dǎo)式,過濾字典
>>>?a_dict?=?{'one':?1,?'two':?2,?'thee':?3,?'four':?4}
>>>?new_dict?=?{k:?v?for?k,?v?in?a_dict.items()?if?v?<=?2}
>>>?new_dict
{'one':?1,?'two':?2}
利用字典推導(dǎo)式,做一些計算
>>>?incomes?=?{'apple':?5600.00,?'orange':?3500.00,?'banana':?5000.00}
>>>?total_income?=?sum([value?for?value?in?incomes.values()])
>>>?total_income
14100.0
字典排序
>>?incomes?=?{'apple':?5600.00,?'orange':?3500.00,?'banana':?5000.00}
>>>?sorted_income?=?{k:?incomes[k]?for?k?in?sorted(incomes)}
>>>?sorted_income
{'apple':?5600.0,?'banana':?5000.0,?'orange':?3500.0}
內(nèi)置函數(shù),與字典配合使用
map()函數(shù)
>>>?prices?=?{'apple':?0.40,?'orange':?0.35,?'banana':?0.25}
>>>?def?discount(current_price):
...?????return?(current_price[0],?round(current_price[1]?*?0.95,?2))
...
>>>?new_prices?=?dict(map(discount,?prices.items()))
>>>?new_prices
{'apple':?0.38,?'orange':?0.33,?'banana':?0.24}
filter()函數(shù)
>>>?prices?=?{'apple':?0.40,?'orange':?0.35,?'banana':?0.25}
>>>?def?has_low_price(price):
...?????return?prices[price]?0.4
...
>>>?low_price?=?list(filter(has_low_price,?prices.keys()))
>>>?low_price
['orange',?'banana']
字典解包運算符
>>>?vegetable_prices?=?{'pepper':?0.20,?'onion':?0.55}
>>>?fruit_prices?=?{'apple':?0.40,?'orange':?0.35,?'pepper':?.25}
>>>?{**vegetable_prices,?**fruit_prices}
{'pepper':?0.25,?'onion':?0.55,?'apple':?0.4,?'orange':?0.35}

E?N?D

各位伙伴們好,詹帥本帥搭建了一個個人博客和小程序,匯集各種干貨和資源,也方便大家閱讀,感興趣的小伙伴請移步小程序體驗一下哦!(歡迎提建議)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
推薦閱讀
牛逼!Python常用數(shù)據(jù)類型的基本操作(長文系列第①篇)
牛逼!Python的判斷、循環(huán)和各種表達式(長文系列第②篇)
評論
圖片
表情
