python字典get()方法用法分析
作者:huaweitman 时间:2023-04-01 23:50:41
本文实例讲述了python字典get()方法用法。分享给大家供大家参考。具体分析如下:
如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法。
这里给大家分享的就是字典的get()方法。
这里我们可以用字典做一个小游戏,假设用户在终端输入字符串:"1"或者是"2"或者是"3",返回对应的内容,如果是输入其他的,则返回"error"
>>> info = {'1':'first','2':'second','3':'third'}
>>> number = raw_input('input type you number:')
input type you number:3
>>> print info.get(number,'error')
third
>>> number = raw_input('input type you number:')
input type you number:4
>>> print info.get(number,'error')
error
希望本文所述对大家的Python程序设计有所帮助。
标签:python,字典,get
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django 实现简单的插入视频
2023-12-24 09:59:33
php线性表顺序存储实现代码(增删查改)
2023-11-19 06:51:53
YOLOv5车牌识别实战教程(六)性能优化与部署
2022-04-26 12:40:54
![](https://img.aspxhome.com/file/2023/3/69573_0s.png)
Python利用pandas计算多个CSV文件数据值的实例
2022-11-21 23:00:26
python生成九宫格图片
2022-09-09 04:46:34
![](https://img.aspxhome.com/file/2023/5/81475_0s.png)
PHP运行环境配置与开发环境的配置(图文教程)
2024-05-11 09:24:46
![](https://img.aspxhome.com/file/2023/5/126215_0s.png)
TensorFlow的环境配置与安装方法
2022-10-29 12:23:19
![](https://img.aspxhome.com/file/2023/6/113166_0s.png)
Python基于opencv的图像压缩算法实例分析
2022-09-29 11:20:41
python+opencv实现霍夫变换检测直线
2021-07-30 04:08:57
![](https://img.aspxhome.com/file/2023/1/89351_0s.jpg)
SqlServer中的日期与时间函数
2024-01-18 03:25:47
Python两台电脑实现TCP通信的方法示例
2023-09-07 04:17:34
Python实现二维曲线拟合的方法
2021-08-26 12:38:01
GO语言入门学习之基本数据类型字符串
2023-07-16 08:26:31
python数据类型_元组、字典常用操作方法(介绍)
2021-06-22 14:06:05
asp使用shotgraph组件生成数字和字母验证码
2007-09-26 12:26:00
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
2023-08-08 01:11:57
python实现批量命名照片
2021-11-03 18:07:42
![](https://img.aspxhome.com/file/2023/3/77043_0s.png)
XML卷之实战锦囊(1):动态排序
2008-09-05 17:20:00
使用tf.keras.MaxPooling1D出现错误问题及解决
2021-02-07 05:01:28
![](https://img.aspxhome.com/file/2023/1/99241_0s.png)
ASP四级联动做法
2009-07-03 15:35:00