创建字典
user_info={"name":"wang","age":"26","gender":"man"}
#我们用键值对来表示
key:name,age,gender
value:wang,26,man
clear(self) 移除字典中所有的元素
user_info={"name":"wang","age":"26","gender":"man"}
user_info.clear()
print(user_info)
#输出:{}
get(self, k, d=None) 根据k返回字典中所对应的值
user_info={"name":"wang","age":"26","gender":"man"}
str1= user_info.get("name")
print(str1)
#输出:wang
items(self) 返回字典中所有的键值对
user_info={"name":"wang","age":"26","gender":"man"}
str1= user_info.items()
print(str1)
#输出:dict_items([('name', 'wang'), ('gender', 'man'), ('age', '26')])
keys(self) 返回字典中所有的key
user_info={"name":"wang","age":"26","gender":"man"}
str1= user_info.keys()
print(str1)
#输出:dict_keys(['name', 'gender', 'age'])
values(self) 返回字典中所有的value
user_info={"name":"wang","age":"26","gender":"man"}
str1= user_info.values()
print(str1)
#输出:dict_values(['26', 'wang', 'man'])
in 可见检查指定key是否在字典中
user_info={"name":"wang","age":"26","gender":"man"}
print("age" in user_info.keys())
#输出:True
del:删除字典中的键值对
dic={"k1":11,"k2":22,"k3":33} del dic["k2"] print(dic) #输出{'k1': 11, 'k3': 33} #如果删除的key不在字典中那么就会报错 dic={"k1":11,"k2":22,"k3":33} del dic["k5"] print(dic) #输出:KeyError: 'k5'
update(self, E=None, **F): 更新字典(个人感觉没什么卵用)
dic={"k1":11,"k2":22,"k3":33} dic.update({"k1":11,"k2":22,"k3":33,"k4":55}) print(dic) #输出:{'k2': 22, 'k4': 55, 'k3': 33, 'k1': 11}