描述
方法get()返回给定键的值。如果键不可用,则返回默认值无。
用法
以下是语法get()方法 -
dict.get(key, default=None)
参数
key- 这是要在字典中搜索的 Key。
default- 这是在键不存在的情况下要返回的值。
返回值
此方法返回给定键的值。如果 key 不可用,则返回默认值 None。
示例
下面的例子展示了 get() 方法的用法。
#!/usr/bin/python3
dict = {'Name':'Zara', 'Age':27}
print ("Value:%s" % dict.get('Age'))
print ("Value:%s" % dict.get('Sex', "NA"))
结果
当我们运行上面的程序时,它会产生以下结果 -
Value:27 Value:NA
相关用法
- Python 3 dictionary cmp()用法及代码示例
- Python 3 dictionary fromkeys()用法及代码示例
- Python 3 dictionary setdefault()用法及代码示例
- Python 3 dictionary update()用法及代码示例
- Python 3 dictionary str()用法及代码示例
- Python 3 dictionary type()用法及代码示例
- Python 3 dictionary has_key()用法及代码示例
- Python 3 Number tan()用法及代码示例
- Python 3 os.fstatvfs()用法及代码示例
- Python 3 List pop()用法及代码示例
- Python 3 os.minor()用法及代码示例
- Python 3 String isupper()用法及代码示例
- Python 3 os.close()用法及代码示例
- Python 3 List index()用法及代码示例
注:本文由纯净天空筛选整理自 Python 3 - dictionary get() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。