数据类型之字典
字典
帮助用户去表示一个事物的信息(事物是有多个属性)。
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'} # 键值
# 请输出:我今天点%s,他的年龄是%s,性别是%s,他喜欢他的%s;
基本格式
data = {键:值,键:值,键:值,键:值,键:值,键:值,}
帮助用户去表示一个事物的信息(事物是有多个属性)。
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'} # 键值
# 请输出:我今天点%s,他的年龄是%s,性别是%s,他喜欢他的%s;
基本格式
data = {键:值,键:值,键:值,键:值,键:值,键:值,}
1. 独有功能
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
1.keys,获取字典中所有的键。 ['name','age','gender','hobby']
1.keys,获取字典中所有的键。 ['name','age','gender','hobby']
for i in info.keys():
print(i)
print(i)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
name
age
gender
hobby
name
age
gender
hobby
2.values,获取字典中所有的值。 ['sundy','18','男','同桌']
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for i in info.values():
print(i)
for i in info.values():
print(i)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
sundy
18
男
同桌
sundy
18
男
同桌
3.items,获取字典中的所有键值对
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for i in info.items():
print(i)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
('name', 'sundy')
('age', 18)
('gender', '男')
('hobby', '同桌')
for i in info.items():
print(i)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
('name', 'sundy')
('age', 18)
('gender', '男')
('hobby', '同桌')
4.get 获取键值,不存在key,则返回None
info = {"k1":"a","name":"sundy","age":1}
print(info["k1"])
#print(info["k1111"]) #没有存在key报错
print(info.get('k1'))
print(info.get('k21',666)) #没有存在key,返回666
print(info.get('k11')) #没有存在key,返回None,None是python的空
info = {"k1":"a","name":"sundy","age":1}
print(info["k1"])
#print(info["k1111"]) #没有存在key报错
print(info.get('k1'))
print(info.get('k21',666)) #没有存在key,返回666
print(info.get('k11')) #没有存在key,返回None,None是python的空
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
a
a
666
None
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
a
a
666
None
5.pop删除键值对
info = {"k1":"a","name":"sundy","age":1}
result=info.pop("k1")
print(info,result)
info = {"k1":"a","name":"sundy","age":1}
result=info.pop("k1")
print(info,result)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
{'name': 'sundy', 'age': 1} a
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
{'name': 'sundy', 'age': 1} a
6.update 不存在键值对则添加,存在则更新
info = {"k1":"a","name":"sundy","age":1}
result=info.update({"k2":"a1","k3":"a2"})
print(info)
result=info.update({"k2":"a1","k3":"a2"})
print(info)
结果:
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
{'k1': 'a', 'name': 'sundy', 'age': 1, 'k2': 'a1', 'k3': 'a2'}
D:\python3.6\python3.6.exe D:/python_code/day01/day06.py
{'k1': 'a', 'name': 'sundy', 'age': 1, 'k2': 'a1', 'k3': 'a2'}
2. 公共功能
1.长度len
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
print(len(info))
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
4
4
2.索引
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
print(info['name'])
print(info['age'])
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
print(info['name'])
print(info['age'])
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
sundy
18
sundy
18
3.for循环
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for item in info.keys():
print(item)
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for item in info.keys():
print(item)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
name
age
gender
hobby
name
age
gender
hobby
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for item in info.values():
print(item)
for item in info.values():
print(item)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
sundy
18
男
同桌
sundy
18
男
同桌
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
for k,v in info.items():
print(k,v)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
name sundy
age 18
gender 男
hobby 同桌
name sundy
age 18
gender 男
hobby 同桌
for 循环判断
v = {'k1':'v1','k2':'v2','k3':'v3'}
# 默认按照键判断,即:判断x是否是字典的键。
if 'x' in v:
pass
# 请判断:k1 是否在其中?
if 'k1' in v:
pass #
请判断:v2 是否在其中?
# 方式一:循环判断
flag = '不存在'
for v in v.values():
if v == 'v2':
flag = '存在'
print(flag)
# 默认按照键判断,即:判断x是否是字典的键。
if 'x' in v:
pass
# 请判断:k1 是否在其中?
if 'k1' in v:
pass #
请判断:v2 是否在其中?
# 方式一:循环判断
flag = '不存在'
for v in v.values():
if v == 'v2':
flag = '存在'
print(flag)
4.修改(存在就修改/不存在就增加)
#修改键值
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
info["name"]="amy"
print(info)
#修改键值
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
info["name"]="amy"
print(info)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
{'name': 'amy', 'age': 18, 'gender': '男', 'hobby': '同桌'}
{'name': 'amy', 'age': 18, 'gender': '男', 'hobby': '同桌'}
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
info["addr"]="beijing"
print(info)
info["addr"]="beijing"
print(info)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
{'name': 'sundy', 'age': 18, 'gender': '男', 'hobby': '同桌', 'addr': 'beijin
#修改键
删除后添加
{'name': 'sundy', 'age': 18, 'gender': '男', 'hobby': '同桌', 'addr': 'beijin
#修改键
删除后添加
删除(键值对)
info = {"name":'sundy','age':18,'gender':'男','hobby':'同桌'}
del info["name"]
print(info)
del info["name"]
print(info)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
{'age': 18, 'gender': '男', 'hobby': '同桌'}
{'age': 18, 'gender': '男', 'hobby': '同桌'}
重点
数据类型嵌套:int bool str list tuple dict
list tuple dict可以多层嵌套:
val = (1,['sundy','a',1],{"k1":"a","k2":(2,3,"sundy")})
打印出(2,3,"sundy")中3
print(val[2]["k2"][1])
打印出(2,3,"sundy")中3
print(val[2]["k2"][1])
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
3
3
#在k2中第一个元素加入a
val = (1,['sundy','a',1],{"k1":"a","k2":[2,3,"sundy"]})
val[2]["k2"].insert(0,"a")
print(val)
D:\python3.6\python3.6.exe D:/python_code/day01/day05.py
(1, ['sundy', 'a', 1], {'k1': 'a', 'k2': ['a', 2, 3, 'sundy']}
(1, ['sundy', 'a', 1], {'k1': 'a', 'k2': ['a', 2, 3, 'sundy']}
总结:
字典的值可以为int bool str list tuple dict类型
字典的key:列表和字典不能为key,其他类型可以
字典的值可以为int bool str list tuple dict类型
字典的key:列表和字典不能为key,其他类型可以
列表,元组:有序
字典:无序(3.6之后字典有序)
字典:无序(3.6之后字典有序)