详解Python中字典的增删改查
作者:陈小c 时间:2021-09-14 19:53:34
Python中的字典
一、字典的特点
二、创建字典
创建字典用大括号表示
dict1={'a':3,'b':4,'c':7} ##第一种创建方式
print(dict1)
dict2=dict(a=2,b=3) ##第一种创建方式
print(dict2)
{'a': 3, 'b': 4, 'c': 7}
{'a': 2, 'b': 3}
三、查询字典数据
dict2=dict(a=2,b=3) ##创建字典
print(dict2['a']) ##查询字典中a的值,如果key不存在,抛出异常
print(dict2.get('a')) ##使get方法查询字典中a的值,如果key不存在,返回none
2
2
四、Key的判断
dict2=dict(a=2,b=3)
print( 'a' in dict2)
print( 'a' not in dict2)
True
False
五、Key的删除
dict2=dict(a=2,b=3) ##删除,清空为clear
del dict2['a']
print(dict2)
{'b': 3}
六、Key的添加
dict2=dict(a=2,b=3) ##
dict2['c']=4
print( dict2 )
{'a': 2, 'b': 3, 'c': 4}
七、Key的修改
dict2=dict(a=2,b=3) ##第一种创建方式
dict2['c']=4 ##添加c,值为4
print( dict2 )
dict2['c']=10 ##修改c的值为10
print( dict2 )
{'a': 2, 'b': 3, 'c': 4}
{'a': 2, 'b': 3, 'c': 10} ##c已经被修改为10
八、字典的视图
keys()
获取字典中所有的key
test={'张三':56,'李斯':56}
print(test.keys())
print(list(test.keys()) ##转换为列表
dict_keys(['张三', '李斯'])
['张三', '李斯']
values()
获取字典中所有的values
values=test.values()
print(values)
dict_values([56, 56])
[56, 56]
iterms()
获取字典中所有的key,values对
iterm=test.items()
print(iterm)
print(list(iterm)) ##转换之后的列表元素是一组元祖
dict_items([('张三', 56), ('李斯', 56)])
[('张三', 56), ('李斯', 56)]
九、字典的遍历
dic1={'a':2,'b':3,'c':4}
for i in dic1:
print(i) ##遍历字典的key
print(dic1.get(i)) ##使用get函数打印值
print(dic1[i]) ##第二种方法遍历值
十、生成字典的表达式
name=['张三','李斯','王五']
age=[23,45,37]
d= {name:age for name,age in zip(name,age)} #注意最外面要加大括号
print(d)
```
d= {test:age for test,age in zip(name,age)} ##跟变量名无关,zip打包的值有关
print(d) ##结果相同
```
{'张三': 23, '李斯': 45, '王五': 37}
外面要加大括号
print(d)
```
d= {test:age for test,age in zip(name,age)} ##跟变量名无关,zip打包的值有关
print(d) ##结果相同
```
{'张三': 23, '李斯': 45, '王五': 37}
来源:https://blog.csdn.net/weixin_43143310/article/details/122282314
标签:Python,字典,增删改查
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
redis之django-redis的简单缓存使用
2023-11-20 10:30:15
浅谈pandas用groupby后对层级索引levels的处理方法
2022-10-15 01:43:37
![](https://img.aspxhome.com/file/2023/7/105787_0s.jpg)
Python操作Sqlite正确实现方法解析
2023-01-24 14:23:23
python爬虫实战项目之爬取pixiv图片
2021-04-17 00:01:29
![](https://img.aspxhome.com/file/2023/8/125188_0s.jpg)
Oracle 启动例程 STARTUP参数说明
2009-07-07 10:26:00
Javascript实现通过选择周数显示开始日和结束日的实现代码
2023-08-23 13:44:52
浅谈python中列表、字符串、字典的常用操作
2023-02-02 23:59:15
轻松处理Dreamweaver段落缩进
2007-11-17 07:53:00
Python 实现中值滤波、均值滤波的方法
2022-05-09 08:58:38
![](https://img.aspxhome.com/file/2023/0/121100_0s.jpg)
php ajax异步读取rss文档数据
2023-10-17 19:59:02
jupyter notebook 调用环境中的Keras或者pytorch教程
2022-10-07 23:23:09
![](https://img.aspxhome.com/file/2023/5/92525_0s.jpg)
Python利用treap实现双索引的方法
2022-02-28 15:59:44
![](https://img.aspxhome.com/file/2023/8/80748_0s.jpg)
一文详述 Python 中的 property 语法
2023-04-03 03:38:01
Scrapy框架爬取Boss直聘网Python职位信息的源码
2022-06-30 21:02:47
![](https://img.aspxhome.com/file/2023/1/111931_0s.png)
python如何支持并发方法详解
2021-05-29 16:50:17
Python Beautiful Soup模块使用教程详解
2021-07-21 16:00:10
![](https://img.aspxhome.com/file/2023/9/117889_0s.png)
Python 实现PS滤镜的旋涡特效
2022-03-03 02:53:52
![](https://img.aspxhome.com/file/2023/7/70067_0s.png)
Python对FTP交互封装的实现
2023-05-18 15:12:14
表单设计中的网页视觉体验
2008-06-26 13:35:00
![](https://img.aspxhome.com/file/UploadPic/20086/26/2008626134221567s.jpg)
Python真题案例之二分法查找详解
2023-09-23 01:39:07
![](https://img.aspxhome.com/file/2023/6/85376_0s.png)