Python访问字典中的值:
# 使用字典 ['键'] 获取字典中的元素
dic = {'a':123,'b':456,'c':789}
print(dic['a'])
# 123
print(dic['c'])
# 789
修改字典元素:
dic = {'a': 123, 'b': 456, 'c': 789} dic['b'] = 14 print(dic) # {'a': 123, 'b': 14, 'c': 789}
增加字典元素:
# 增加字典元素 dic = {'a':123,'b':456,'c':789} dic['d'] = 555 print(dic) # {'a': 123, 'b': 456, 'c': 789, 'd': 555}
删除操作:
dic = {'a': 123, 'b': 14, 'c': 789} # 删除字典元素 del dic['b'] print(dic) # {'a': 123, 'c': 789} # 清空字典 dic = {'a': 123, 'b': 14, 'c': 789} dic.clear() print(dic) # {} # 删除字典 dic = {'a': 123, 'b': 14, 'c': 789} del dic
2020-02-09