美文网首页
Python 字典(Dictionary) 中函数get()的用

Python 字典(Dictionary) 中函数get()的用

作者: KangSmit的算法那些事儿 | 来源:发表于2020-06-27 12:59 被阅读0次

描述

Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。

语法:

get()方法语法:

dict.get(key, default=None)

参数:

key -- 字典中要查找的键。
default -- 如果指定键的值不存在时,返回该默认值。

返回值:

返回指定键的值,如果值不在字典中返回默认值None。

实例

以下实例展示了 get()函数的使用方法:

#获取指定关键字或值
dict = {'Name': 'Runoob', 'Age': 20}

print("Value : %s" %  dict.get('Age'))
#也可另取字典,在get函数中可直接用半圆括号
print("Value : %s" %  dict.get('Sex', "Never"))

以上实例输出结果为:


image.png

相关文章

网友评论

      本文标题:Python 字典(Dictionary) 中函数get()的用

      本文链接:https://www.haomeiwen.com/subject/xzpmfktx.html