1.查询
2.修改3.删除4.增加5.清空例如:
dict1 = { 'k1':'jin','k2':123, 'k3':['tong',200]}1.1字典查询有哪些键
print(dict1.keys())dict_keys(['k2', 'k1', 'k3'])1.2字典查询值
print(dict1.values())dict_values([123, 'jin', ['tong', 200]])1.3 根据key 查value
print(dict1["k1"])print(dict1["k1"]) #在查询不到时会报错print(dict1.get("k3"))if 'k3' in dict1: print("true") 2.字典修改dict1['k1'] = 'JIN'print(dict1.get("k1"))输出:JIN3、删除的三种方法
3.1 pop 方法dict1.pop('k1') #常用方法print(dict1)输出:{'k3': ['tong', 200], 'k2': 123}3.2 del 方法
del dict1['k1']print(dict1){'k3': ['tong', 200], 'k2': 123}输出:{'k3': ['tong', 200], 'k2': 123}3.3 popitem 方法
dict1.popitem()print(dict1)输出:{'k1': 'jin', 'k2': 123}
4.增加
dict1['k4'] = 'Li'
print(dict1){'k3': ['tong', 200], 'k1': 'jin', 'k2': 123, 'k4': 'Li'}
5.清空
dict1.clear()