Python:互换字典的键值对
2018-03-13 19:12
429 查看
1.zip
2.循环
3.列表生成器
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.循环
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实现字典的键值对互换的三种方法
- python通过字典dict判断指定键值是否存在的方法
- python中字典按键或键值排序
- 浅谈python字典多键值及重复键值的使用
- 在Python中用get()方法获取字典键值的教程
- python字典键值对的添加和遍历方法
- python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
- Python合并字典键值并去除重复元素的实例
- python 零散记录(四) 强调字典中的键值唯一性 字典的一些常用方法
- python2/3 中删除字典中value为空的键值对方法
- Python轻松入门-36 字典的多键值排序
- Python中对字典按其键值进行排序
- 004_014 Python 反转字典 即key和val互换
- python 字典对根据键值进行的小操作
- python:二维字典的键值合并等函数
- Python学习实践----比较字典中是否有相同键值的方法之一
- Python中字典容器dict默认按照键值从大到小存储的证明
- 浅谈python字典多键值及重复键值的使用
- python字典多键值及重复键值的使用
- python字典英雄技能,键值对特点,删除,遍历取键值