python 字典 get()方法
2013-07-26 11:56
645 查看
如果我们需要获取字典值的话,我们有两种方法,一个是通过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
今天给大家分享的就是字典的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字典(dictionary)中get和items、iteritems方法
- Python 字典(Dictionary) get()方法
- Python 字典(Dictionary) get()方法
- Python 字典(Dictionary) get()方法
- Python中字典的增、删、创建、索引与字典方法clear,copy,formkeys,get,has_key,popitem,update
- 【转】Python的字典get方法:从字典中获取一个值
- Python的字典get方法:从字典中获取一个值
- Python 字典的 get() 方法和 [key] 方法对比
- Python 字典(Dictionary) get()方法
- python 字典 get(),items(),iteritems()方法
- Python 中dict字典get方法讲解
- Python 字典(Dictionary) get()方法
- python字典get()方法用法分析
- Python 字典(Dictionary) get()方法
- Python 字典(Dictionary) get()方法
- python-字典get方法
- python 字典的get方法
- Python中字典get方法的使用
- 【python】Python的字典get方法:从字典中获取一个值
- python字典(dict)get方法