python中的字典操作及字典函数


Posted in Python onJanuary 03, 2018

字典

dict_fruit = {'apple':'苹果','banana':'香蕉','cherry':'樱桃','avocado':'牛油果','watermelon':'西瓜'}

字典的操作

#字典的遍历方式 
#默认遍历(遍历key) 
for value in dict_fruit: 
  print(value) 
''''' 
遍历出的值: 
watermelon 
apple 
cherry 
avocado 
banana 
''' 
#使用key遍历(与默认遍历一样) 
for key in dict_fruit.keys(): 
  print(key) 
''''' 
遍历出的值: 
watermelon 
apple 
cherry 
avocado 
banana 
''' 
#使用value遍历 
for value in dict_fruit.values(): 
  print(value) 
''''' 
遍历出的值: 
苹果 
牛油果 
香蕉 
西瓜 
樱桃 
''' 
#使用key,value遍历 
for key,value in dict_fruit.items(): 
  print(key+'--->'+value) 
''''' 
遍历出的值: 
avocado--->牛油果 
apple--->苹果 
banana--->香蕉 
cherry--->樱桃 
watermelon--->西瓜 
''' 
#创建字典 
#使用dict() 
res = dict(brand = '品牌',size='尺码',color='颜色') 
print(res,type(res)) 
''''' 
res结果: 
{'size': '尺码', 'brand': '品牌', 'color': '颜色'} <class 'dict'> 
''' 
#使用zip()和dict() 
keys = ['1','2','3','4','5'] 
values = [1,2,3,4,5] 
res = dict(zip(keys,values)) 
print(res,type(res)) 
''''' 
res结果: 
{'3': 3, '4': 4, '1': 1, '2': 2, '5': 5} <class 'dict'> 
''' 
#字典的推导式 
res = {k+'的中文是'+v for k,v in dict_fruit.items()} 
print(res) 
''''' 
res结果: 
{'watermelon的中文是西瓜', 'avocado的中文是牛油果', 'banana的中文是香蕉', 'cherry的中文是樱桃', 'apple的中文是苹果'} 
'''

字典的函数

#清空字典 
test1 = {1:'1'} 
test1.clear() 
print(test1) 
''''' 
test1结果: 
{} 
''' 
#复制字典(复制成一个新字典) 
test2 = {2:'2'} 
test2_copy = test2.copy() 
print(test2_copy) 
''''' 
test2结果: 
{2: '2'} 
''' 
#使用指定的key和value制作一个字典 
list_test = ['a','b','c'] 
test3 = {}.fromkeys(list_test,'ojbk') 
print(test3) 
''''' 
test3结果: 
{'a': 'ojbk', 'b': 'ojbk', 'c': 'ojbk'} 
''' 
#将一个字典转化为二级容器(中间容器) 
res = dict_fruit.items() 
print(res,type(res)) 
''''' 
res结果: 
dict_items([('avocado', '牛油果'), ('apple', '苹果'), ('banana', '香蕉'), ('watermelon', '西瓜'), ('cherry', '樱桃')]) <class 'dict_items'> 
''' 
#将字典的key组成新的容器 
res = dict_fruit.keys() 
print(res,type(res)) 
''''' 
res结果: 
dict_keys(['watermelon', 'cherry', 'avocado', 'apple', 'banana']) <class 'dict_keys'> 
''' 
#将字典的value组成新的容器 
res = dict_fruit.values() 
print(res,type(res)) 
''''' 
res结果: 
dict_values(['牛油果', '香蕉', '樱桃', '苹果', '西瓜']) <class 'dict_values'> 
''' 
#根据key删除字典中的数据 
test4 = {1:'1',2:'2',3:'3'} 
test4.pop(2) 
print(test4) 
''''' 
test4结果: 
{1: '1', 3: '3'} 
''' 
#依次弹出(删除)字典中的数据 
test5 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
test5.popitem() 
print(test5) 
test5.popitem() 
print(test5) 
test5.popitem() 
print(test5) 
''''' 
test5依次结果: 
{2: '2', 3: '3', 4: '4', 5: '5'} 
{3: '3', 4: '4', 5: '5'} 
{4: '4', 5: '5'} 
''' 
#更新dict中的数据(更新一个不存在的key时,可用于添加新数据) 
test6 = {'super':'Eric','ssuper':'Cbabe','sssuper':'Gogo','supreme':'wiz333'} 
#更新数据 
test6.update(super='Eric-LPL') 
print(test6) 
#添加数据 
test6.update(niceboy='Bigmao') 
print(test6) 
''''' 
test6依次结果: 
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} 
{'ssuper': 'Cbabe', 'supreme': 'wiz333', 'niceboy': 'Bigmao', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} 
''' 
#获取dict中的数据(使用key获取) 
test7 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
res = test7.get(1) 
print(res,type(res)) 
''''' 
test7结果: 
1 <class 'str'> 
''' 
#给dict添加数据(setdefault,不能用于更新数据) 
test8 = {1:'1',2:'2',3:'3',4:'4',5:'5'} 
test8.setdefault(6,'6') 
print(test8) 
''''' 
test8结果: 
{1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6'} 
'''

总结

以上所述是小编给大家介绍的python中的字典操作及字典函数,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
使用Python编写提取日志中的中文的脚本的方法
Apr 30 Python
python 编程之twisted详解及简单实例
Jan 28 Python
Python:Scrapy框架中Item Pipeline组件使用详解
Dec 27 Python
ActiveMQ:使用Python访问ActiveMQ的方法
Jan 30 Python
python 通过SSHTunnelForwarder隧道连接redis的方法
Feb 19 Python
CentOS7安装Python3的教程详解
Apr 10 Python
springboot配置文件抽离 git管理统 配置中心详解
Sep 02 Python
python基于TCP实现的文件下载器功能案例
Dec 10 Python
基于python使用tibco ems代码实例
Dec 20 Python
python使用Thread的setDaemon启动后台线程教程
Apr 25 Python
pyqt5打包成exe可执行文件的方法
May 14 Python
使用python绘制分组对比柱状图
Apr 21 Python
Python将多个excel表格合并为一个表格
Feb 22 #Python
使用Python+Splinter自动刷新抢12306火车票
Jan 03 #Python
Python实现简易Web爬虫详解
Jan 03 #Python
Python读取MRI并显示为灰度图像实例代码
Jan 03 #Python
使用 Python 实现微信公众号粉丝迁移流程
Jan 03 #Python
EM算法的python实现的方法步骤
Jan 02 #Python
Python+树莓派+YOLO打造一款人工智能照相机
Jan 02 #Python
You might like
从零开始学YII2框架(二)通过 Composer 安装扩展插件
2014/08/20 PHP
php使用Cookie实现和用户会话的方法
2015/01/21 PHP
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
2015/07/29 PHP
Yii框架Session与Cookie使用方法示例
2019/10/14 PHP
一端时间轮换的广告
2006/06/26 Javascript
js wmp操作代码小结(音乐连播功能)
2008/11/08 Javascript
自己的js工具 Event封装
2009/08/21 Javascript
默认让页面的第一个控件选中的javascript代码
2009/12/26 Javascript
吐槽一下我所了解的Node.js
2014/10/08 Javascript
使用javascript实现雪花飘落的效果
2015/01/13 Javascript
vue,angular,avalon这三种MVVM框架优缺点
2016/04/27 Javascript
jQuery实现侧浮窗与中浮窗切换效果的方法
2016/09/05 Javascript
node.js + socket.io 实现点对点随机匹配聊天
2017/06/30 Javascript
微信小程序实现跟随菜单效果和循环嵌套加载数据
2017/11/21 Javascript
JS表单传值和URL编码转换
2018/03/03 Javascript
JS实现的全选、全不选及反选功能【案例】
2019/02/19 Javascript
js实现盒子移动动画效果
2020/08/09 Javascript
[37:50]VP vs TNC Supermajor小组赛B组 BO3 第一场 6.2
2018/06/03 DOTA
Python设计模式之观察者模式实例
2014/04/26 Python
分享Python开发中要注意的十个小贴士
2016/08/30 Python
Python cookbook(数据结构与算法)字典相关计算问题示例
2018/02/18 Python
python自动发邮件库yagmail的示例代码
2018/02/23 Python
Python中elasticsearch插入和更新数据的实现方法
2018/04/01 Python
对Python中range()函数和list的比较
2018/04/19 Python
浅谈Python中的继承
2020/06/19 Python
python 使用多线程创建一个Buffer缓存器的实现思路
2020/07/02 Python
基于css3 animate制作绚丽的动画效果
2015/11/24 HTML / CSS
css3针对移动端卡顿问题的解决(动画性能优化)
2020/02/14 HTML / CSS
浅谈amaze-ui中datepicker和datetimepicker注意的几点
2020/08/21 HTML / CSS
日本高岛屋百货购物网站:TAKASHIMAYA
2019/03/24 全球购物
酒店仓管员岗位职责
2014/04/28 职场文书
法人任命书范本
2014/06/04 职场文书
土建施工员岗位职责
2014/07/16 职场文书
党性分析材料格式
2014/12/19 职场文书
建党伟业电影观后感
2015/06/01 职场文书
SQL Server中锁的用法
2022/05/20 SQL Server