一、字典操作
1.往字典中添加键值对
- dict["key"] = "value"
stu = {"name":"zhangsan","age":"18"}
stu["sex"] = "boy"
print (stu)
- setdefault()
- 有返回值
stu = {"name":"zhangsan","age":"18"}
stu.setdefault("sex")
print (stu) #结果 {'age': '18', 'name': 'zhangsan', 'sex': None}
stu = {"name":"zhangsan","age":"18"}
stu.setdefault("sex","boy")
print (stu) #结果 {'age': '18', 'name': 'zhangsan', 'sex': 'boy'}
#添加的键存在时
stu = {"name":"zhangsan","age":"18"}
ret = stu.setdefault("age","20")
print(ret)
print(stu)
---> 18 #setdefault 返回值为改键已存在的值
---> {'age': '18', 'name': 'zhangsan'} #键中的值不会被修改
#添加的键不存在时
stu = {"name":"zhangsan","age":"18"}
ret = stu.setdefault("sex","boy")
print(ret)
print(stu)
---> boy #setdefault 返回值为被添加键所对应的值
---> {'age': '18', 'sex': 'boy', 'name': 'zhangsan'}
2.替换字典中指定键的值
stu = {"name":"zhangsan","age":18,"sex":"boy"}
#替换值boy为gril
stu["sex"] = "girl"
print (stu)
3.update()一个字典所有项追加到另一个字典里
a = {"name":"zhangsan","sex":"boy"}
b = {"age":18,"name":"lisi"}
a.update(b) #往字典a中追加字典b的内容
print (a) #当追加的键存在,则会覆盖之前的键所对应的值
print (b)
---> {'sex': 'boy', 'age': 18, 'name': 'lisi'}
---> {'age': 18, 'name': 'lisi'}
4.字典取值
- get()函数 --> 当提取的键不存在时,不会报错,为空
a = {"name":"zhangsan","sex":"boy"}
print (a.get("name"))
#当取的键不存在时,不报错,返回值为None
print (a.get("age"))
#示例
"""
dic = {"name1":"zhangsan","age":{"age1":"18","age2":20},"name2":"lisi","name3":"wangwu"}
"""
- dict["key"]
a = {"name":"zhangsan","sex":"boy"}
print (a["name"])
5.keys()从字典中取出所有键
- 不能通过在括号中输入键来取键值
- 返回所有的键,类型为 "dict_keys"
stu = {"name":"zhangsan","age":"18"}
key = stu.keys()
print(key) #打印出所有键序列
print(type(key)) #返回类型为 dict_keys
print(list(key)) #将 dict_keys 类型转换成列表
---> dict_keys(['age', 'name'])
---> <class 'dict_keys'>
---> ['age', 'name']
6.values() 从字典中取出所有值
- 返回所有的值,类型为 "dict_values"
stu = {"name":"zhangsan","age":"18"}
value = stu.values()
print(value) #打印出所有值序列
print(type(value)) #返回类型为 dict_values
print(list(value)) #将 dict_values 类型转换成列表
---> dict_values(['zhangsan', '18'])
---> <class 'dict_values'>
---> ['zhangsan', '18']
7.items()以 dict_items 返回可遍历的(键, 值) 元组数组
info = {"name":"zhangsan","sex":"boy"}
print(info.items())
print(type(info.items()))
---> dict_items([('sex', 'boy'), ('name', 'zhangsan')])
---> <class 'dict_items'>
info = {"name":"zhangsan","sex":"boy"}
for i in info.items():
print(i)
---> ('name', 'zhangsan')
---> ('sex', 'boy')
#-----------------------------------------
info = {"name":"zhangsan","sex":"boy"}
for x,y in info.items():
print(x,y)
---> name zhangsan
---> sex boy
8.del 删除键值对
a = {"name":"zhangsan","sex":"boy","age":18}
del a["name"]
print (a) #结果中"name":"zhansan"该键值对全部被删除
9.pop()删除键值对
- 括号中必须为键且不能为空
- 返回值为删除键的值
a = {"name":"zhangsan","sex":"boy","age":18}
value = a.pop("name")
print (value) #返回值为"zhangsan"
print (a) #字典a中的"name":"zhangsan"键值对被删除
10.popitem()随机删除字典最后一个键值对
- 并返回该键值对的元组
a = {"name":"zhangsan","sex":"boy","age":18}
b = a.popitem()
print (b) #返回值为包含被删除的键值对的一个元组
print (a) #字典a中的某个键值对被删除
11.clear()清空字典中的键值对
- 没有返回值
- 原字典为空字典{}
a = {"name":"zhangsan","sex":"boy","age":18}
a.clear()
print (a)
12.copy()复制创建字典
a = {"name":"zhangsan","sex":"boy","age":18}
b = a.copy()
print (b) #b是a复制出来的字典
13.has_key()检查字典是否有指定的键,有结果为 True,没有结果为 False
- python3.0 中删除了此方法
a = {"name":"zhangsan","sex":"boy","age":18}
print a.has_key("age") #True
print a.has_key("height") #False
user = raw_input("账号:")
pwd = raw_input("密码:")
mysql.setdefault(user,pwd)
print mysql
if mysql.has_key(user):
print "ok"
else:
print "no"
作者:多测师高级讲师_郑sir
微信:ZhengYing8887
出处:https://www.cnblogs.com/ZhengYing0813/
备注:本文版权归作者所有,欢迎转载和添加作者微信探讨技术,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。