Python取出字典中的值的实现
作者:404.Sunflower 时间:2022-01-09 20:16:07
字典的常用方法
方便举例,先创建2个字典
list_test={"bob":19,"aoa":18,"coc":17}
list_b={'qqq':000}
参数 | 返回值 | 含义 |
---|---|---|
.items() | dict_items([(‘bob’, 19), (‘aoa’, 18), (‘coc’, 17)]) | 返回所有键值 |
.keys() | dict_keys([‘bob’, ‘aoa’, ‘coc’]) | 返回keys值 |
.values() | dict_values([19, 18, 17]) | 返回values值 |
.clear() | 无 | 删除字典中的所有项目 |
.get(key) | 19 | 返回字典中key对应的值,例如list_test.get("bob") |
list_test.pop(key) | 19 | 删除并返回字典中的key对应的值,例如list_test.pop("bob") |
list_test.update(字典) | {‘bob’: 19, ‘aoa’: 18, ‘coc’: 17, ‘qqq’: 0} | 将字典list_test合并list_b,例如list_test.update(list_b) |
输出字典中的值:
list_test={"bob":19,"aoa":18,"coc":17}
for i,j in test.items():
print(i, j)
list_test={"bob":19,"aoa":18,"coc":17}
for i in test :
print(i,test[i])
删除字典中的某一项
del list_test["coc"]
值是否在字典中(in、not in),返回True/False
'bob' in list_test
PS:其他方法
person = {'name':'xiaoming', 'age':18}
# 第一种 若键不存在则会抛出KeyError异常
person['city']
# 第二种 不会抛出异常,不存在则返回None,也可以设置默认返回值
person.get('city',"上海")
# 第三种 与第二种类似,区别在于setdefault方法会更新字典
person.setdefault('city', '上海')
来源:https://blog.csdn.net/weixin_45951067/article/details/120863023
标签:Python,字典,值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python TypeError: ‘float‘ object is not subscriptable错误解决
2023-09-13 05:33:02
![](https://img.aspxhome.com/file/2023/9/62249_0s.png)
Python数据分析之pandas比较操作
2021-11-23 05:14:22
![](https://img.aspxhome.com/file/2023/4/66874_0s.png)
mysql基础教程:安装与操作
2009-07-30 08:18:00
使用Pandas将inf, nan转化成特定的值
2023-04-15 23:36:33
![](https://img.aspxhome.com/file/2023/4/86354_0s.jpg)
asp如何在网站上提供音乐下载?
2010-06-22 21:14:00
Tensorflow的常用矩阵生成方式
2023-03-27 16:03:55
SQL Server 中导入导出数据三方法比较
2009-01-21 14:22:00
网页常用特效整理:初级篇
2023-06-27 00:58:37
python列表详情
2023-12-29 01:59:06
Python将列表数据写入文件(txt, csv,excel)
2023-04-09 13:24:49
使用JScript遍历Request表单参数集合
2011-02-26 11:08:00
asp中去除html中style,javascript,css代码
2011-02-16 11:18:00
TensorFlow中tf.batch_matmul()的用法
2022-06-06 10:33:37
PHP与MySQL开发中页面乱码的产生与解决
2023-10-03 04:10:08
数据库理论:学习基于SQL数据库的算法
2009-01-13 13:38:00
python中如何使用insert函数
2023-08-02 17:04:43
Firefox插件推荐:CSS Usage
2009-10-26 18:44:00
![](https://img.aspxhome.com/file/UploadPic/200910/26/cssusage-70s.jpg)
Pycharm 2020最新永久激活码(附最新激活码和插件)
2023-06-14 14:21:21
![](https://img.aspxhome.com/file/2023/2/59132_0s.jpg)
Python实现平行坐标图的两种方法小结
2023-07-30 20:45:34
![](https://img.aspxhome.com/file/2023/6/61456_0s.jpg)
详解Python如何批量检查图像是否可用
2021-10-03 13:42:07