python字典get()方法用法分析

yipeiwu_com5年前Python基础

本文实例讲述了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中@property和property函数常见使用方法示例

本文实例讲述了python中@property和property函数常见使用方法。分享给大家供大家参考,具体如下: 1、基本的@property使用,可以把函数当做属性用 class...

解决Ubuntu pip 安装 mysql-python包出错的问题

问题描述如下,报没有找到mysql_config环境变量 $ pip install mysql-python Collecting MySQL-python==1.2.5 (f...

python+matplotlib演示电偶极子实例代码

python+matplotlib演示电偶极子实例代码

使用matplotlib.tri.CubicTriInterpolator.演示变化率计算: 完整实例: from matplotlib.tri import ( Triang...

快速了解Python相对导入

1、绝对导入和相对导入 绝对导入:按照sys.path顺序搜索,先主目录(sys.path中第一项''),然后PYTHONPATH环境变量、标准库路径、pth指定路径等。 相对导入:...

Python Multiprocessing多进程 使用tqdm显示进度条的实现

Python Multiprocessing多进程 使用tqdm显示进度条的实现

1.背景 在python运行一些,计算复杂度比较高的函数时,服务器端单核CPU的情况比较耗时,因此需要多CPU使用多进程加快速度 2.函数要求 笔者使用的是:pathos.multipr...