Python 字典 get() 方法
2017-10-26 17:48
447 查看
描述
Python 字典 get() 方法和 setdefault() 方法类似,返回指定键的值,如果键不在字典中,返回一个指定值,默认为None。get() 和 setdefault() 区别: setdefault() 返回的键如果不在字典中,会添加键(更新字典),而 get() 不会添加键。
语法
get() 方法语法:D.get(key[,default=None])
参数
key -- 字典中要查找的键。default -- 可选参数,如果指定键的值不存在时,返回该值,默认为 None。
返回值
返回指定键的值,如果指定键的值不在字典中返回指定值,默认为 None。实例
以下实例展示了 get() 方法的使用方法:#!/usr/bin/python3 D = {'Name': 'Runoob', 'Age': 27} print ("Age 值为 : %s" % D.get('Age')) print ("Sex 值为 : %s" % D.get('Sex', "NA")) print ("Sex 值为 : %s" % D.get('Sex'))
以上实例输出结果为:
Age 值为 : 27 Sex 值为 : NA Sex 值为 : None
相关文章推荐
- python字典结构get()方法的陷阱
- python 字典(dict)get方法应用
- 【转】Python的字典get方法:从字典中获取一个值
- python 字典get方法
- Python中字典的get()方法以及defaultdict
- Python3 字典 get() 方法
- Python 字典(Dictionary) get()方法
- 在Python中用get()方法获取字典键值的教程
- Python 字典(Dictionary) get()方法
- Python中字典的增、删、创建、索引与字典方法clear,copy,formkeys,get,has_key,popitem,update
- Python的字典get方法:从字典中获取一个值
- python 字典的get方法
- python 字典 get(),items(),iteritems()方法
- 【python】Python的字典get方法:从字典中获取一个值
- Python 字典(Dictionary) get()方法
- python 字典的get方法
- python 字典 get()方法
- python字典get()方法用法分析
- python 字典 get方法
- 在Python中用get()方法获取字典键值的教程