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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

Pytorch 实现冻结指定卷积层的参数

python代码 for i, para in enumerate(self._net.module.features.parameters()): if i &...

python版学生管理系统

python版学生管理系统

写一个学生管理系统,最好用python。 我都没学过python呢,只好开始临时抱佛脚,再到网上找找有没有例子看看,下面是我参照另一个博主写的,中间有一些和我不能融合的错误,我已经解决了...

python解析发往本机的数据包示例 (解析数据包)

tcp.py 复制代码 代码如下:# -*- coding: cp936 -*-import socketfrom struct import *from time import cti...

python对视频画框标记后保存的方法

需要画框取消注释rectangle import cv2 import os,sys,shutil import numpy as np # Open the input mov...

python在每个字符后添加空格的实例

实例如下所示: #!/usr/bin/env Python # coding=utf-8 file = open("chinese1.txt",'r') file2 = open(r...