1. 程式人生 > >python中字典的‘增、刪、改、查’

python中字典的‘增、刪、改、查’

python 字典的‘增、刪、改、查’

用 ipython 執行情況如下:
#新建字典
In [1]: infos = {'name':'dongge','age':18}

In [2]: infos
Out[2]: {'name': 'dongge', 'age': 18}

#增加key:QQ
In [3]: infos['QQ'] = 3333333

In [4]: infos
Out[4]: {'name': 'dongge', 'age': 18, 'QQ': 3333333}

#刪除key:age
In [5]: del infos['age']

In [6]: infos
Out[6]: {'name': 'dongge', 'QQ': 3333333}

#修改
In [7]: infos['QQ']= 12312414

In [8]: infos
Out[8]: {'name': 'dongge', 'QQ': 12312414}

#查詢:
In [9]: infos.get('age')#無返回值

In [10]: infos.get('QQ')
Out[10]: 12312414