python dict的get函数

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

可以如下表示,

dict.get(key, default=None)

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

来看个实际例子,

>>> a={"Kobe":24, "T-mac":1}
>>> a.get("Kobe")
24
>>> a.get("AI", 3)
3
>>> a.get("AI", "Allen Iverson")
'Allen Iverson'
>>> 

猜你喜欢

转载自blog.csdn.net/u010039418/article/details/81176254