您可以通过添加新条目或键值对,修改现有条目或删除现有条目来更新字典,如下面的简单示例所示:
#!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8; # update existing entry dict['School'] = "DPS School"; # Add new entry print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']
输出结果
执行以上代码后,将产生以下结果-
dict['Age']: 8 dict['School']: DPS School