首页 > 技术文章 > 字典的两种访问方式

maxiaohei 2017-11-02 22:21 原文

字典的访问方式:

根据键访问值

info = {'name':'班长', 'id':100, 'sex':'f', 'address':'地球亚洲中国北京'} print(info['name']) print(info['address'])

若访问不存在的键,则会报错:

>>> info['age']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'age'

在我们不确定字典中是否存在某个键而又想获取其值时,可以使用get方法,还可以设置默认值:

>>> age = info.get('age')
>>> age #'age'键不存在,所以age为None
>>> type(age)
<type 'NoneType'>
>>> age = info.get('age', 18) # 若info中不存在'age'这个键,就返回默认值18
>>> age
18

推荐阅读