python数据类型之字典(二)

字典的基本操作

键值查找:

>>> aInfo = {'Wangdachui':3000,'Niuyun':2000,'Linling':4500,'Tianqi':8000}
>>> aInfo['Niuyun']
2000

更新:

>>> aInfo['Niuyun'] = 9999
>>> aInfo
{'Wangdachui': 3000, 'Niuyun': 9999, 'Linling': 4500, 'Tianqi': 8000}

添加:

>>> aInfo['Fuyun'] = 1000
>>> aInfo
{'Wangdachui': 3000, 'Niuyun': 9999, 'Linling': 4500, 'Tianqi': 8000, 'Fuyun': 1000}

成员判断:

>>> 'Mayun' in aInfo
False

删除字典成员:

>>> del aInfo['Fuyun']
>>> aInfo
{'Wangdachui': 3000, 'Niuyun': 9999, 'Linling': 4500, 'Tianqi': 8000}

dict2 = dict1 操作将dict2和dict1关联对应到同一个字典,执行例如dict1['score'] = 99的操作后,dict2中的内容会受影响。

>>> dict2 = dict1 = {}
>>> dict1['score'] = 90
>>> dict1
{'score': 90}
>>> dict2
{'score': 90}
>>> dict1['score'] = 80
>>> dict1
{'score': 80}
>>> dict2
{'score': 80}

已有员工姓名和工资信息表{‘Wangdachui’:3000,'Niuyun':2000,'Linling':4500,'Tianqi':8000},如何单独输出员工姓名和工资金额?

>>> aInfo = {'Wangdachui':3000,'Niuyun':2000,'Linling':4500,'Tianqi':8000}
>>> aInfo.keys()
dict_keys(['Wangdachui', 'Niuyun', 'Linling', 'Tianqi'])
>>> aInfo.values()
dict_values([3000, 2000, 4500, 8000])
>>> for k,v in aInfo.items():
print(k,v) Wangdachui 3000
Niuyun 2000
Linling 4500
Tianqi 8000

人事部门有两份人员和工资信息表,第一份是原有信息,第二份是公司中有工资更改人员和新进人员的信息,如何处理可以较快地获得完整的信息表?

>>> aInfo = {'Wangdachui':3000,'Niuyun':2000,'Linling':4500}
>>> bInfo = {'Wangdachui':4000,'Niuyun':9999,'Wangzi':6000}
>>> aInfo.update(bInfo)
>>> aInfo
{'Wangdachui': 4000, 'Niuyun': 9999, 'Linling': 4500, 'Wangzi': 6000}
上一篇:google-breakpad


下一篇:codeforces 600E Lomsat gelral