十五、Python字典
(一)字典理解
字典用于在键值对中存储数据值,字典是有序、可变且不允许重复的集合。(从Python3.7版开始,字典是有序的,在Python3.6及更早版本中,字典是无序的。
1、创建字典与访问
1)基本形式
使用大括号编写,有键和值。并且以键值对的形式呈现,可以用键名进行引用。
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
print(dict)
2)字典是有序、可变并且不允许重复的,重复值会覆盖现有值。
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
print(dict["year"])
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"year":1999
}
print(dict)
2、字典长度
使用len()函数
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"year":1999
}
print(len(dict))
3、数据类型
字典中的值可以是任何数据类型
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"year":1999,
"electric":False,
"colors":["red","white","blue"]
}
print(dict)
print(len(dict))
print(type(dict))
补充:
(二)访问字典
1、访问键名
1)通过引用方括号内的键名来访问字典的项目
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
print(dict["year"])
2)调用get()方法
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
print(dict["year"])
x=dict.get("year")
print(x)
2、访问键值
1)key()方法将返回字典中所有键的列表
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
x=dict.keys()
print(x)
2)向原始字典添加一个新项目,并看到列表也得到更新
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
dict["age"]=22
print(dict)
3)values()方法返回字典所有值的列表
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
x=dict.keys()
y=dict.values()
print(x)
print(y)
4)items方法将返回字典中的每个项目,作为列表的元祖
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
x=dict.keys()
y=dict.values()
z=dict.items()
print(x)
print(y)
print(z)
5)in关键字确定字典中是否存在指定的键
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
x=dict.keys()
y=dict.values()
print(x)
print(y)
if "year" in dict:
print("年龄")
(三)更改字典
1、基本修改
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
dict["age"]=22
print(dict)
2、使用update()方法修改
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
# dict["age"]=22
dict.update({"year":2021})
print(dict)
(四)添加字典内容
1、基本添加
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
dict["age"]=22
print(dict)
2、update方法修改添加
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998
}
dict["age"]=22
print(dict)
dict.update({"age":"20岁"})
print(dict)
(五)删除字典
1)pop()方法删除指定键名的项
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
dict.pop("age")
print(dict)
2)popitem()方法删除最后插入的项目(在3.7版本之前是随机删除项目)
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
dict.popitem()
print(dict)
3)del关键字删除与指定键名称的项目
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
del dict["model"]
print(dict)
4)del关键字完全删除字典
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
del dict
print(dict)
5)clear()清空字典
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
dict.clear()
print(dict)
(六)遍历字典
1、for循环打印键名
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
for i in dict:
print(i)
2、for循环打印键值
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
for i in dict:
print(dict[i])
3、for循环使用values()打印键值
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
for i in dict.values():
print(i)
4、for循环使用keys()打印键名
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
for i in dict.keys():
print(i)
5、for循环使用items()方法遍历
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
for i,j in dict.items():
print(i,j)
(七)复制字典
1、使用copy()函数
dict={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
dictionary=dict.copy()
print(dictionary)
2、内置dict()函数
a={
"brand":"Ford",
"model":"Mustang",
"year":1998,
"age":33
}
mydictionary=dict(a)
print(mydictionary)
出现的错误——重启内核解决
(八)嵌套字典
1、创建一个包含三个字典的字典
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
print(myfamily)
2、创建三个字典,然后创建一个包含三个字典的字典
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
print(myfamily)
(九)练习
答案:
1、
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.get("model"))
2、
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
year=car["year"]=2020
print(car)
3、
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.update({"color":"red"})
print(car)
4、
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
5、
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)