Python 互換字典的鍵值對(duì)實(shí)例
更新時(shí)間:2019年02月12日 08:42:46 作者:喜歡海呀
今天小編就為大家分享一篇Python 互換字典的鍵值對(duì)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
1.zip
dic = {'a':1, 'b':2, 'c':3} dic_new = dict(zip(dic.values(), dic.keys())) print(dic_new) # {1: 'a', 2: 'b', 3: 'c'}
2.循環(huán)
dic = {'a':1, 'b':2, 'c':3} dic_new = {} for key, val in dic.items(): dic_new[val] = key print(dic_new) # {1: 'a', 2: 'b', 3: 'c'}
3.列表生成器
dic_new = dict([val, key] for key, val in dic.items()) print(dic_new) # {1: 'a', 2: 'b', 3: 'c'}
以上這篇Python 互換字典的鍵值對(duì)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
python matplotlib餅狀圖參數(shù)及用法解析
這篇文章主要介紹了python matplotlib餅狀圖參數(shù)及用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11