9.1 字典的基本概念
基本形式:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
字典用于在键值对中存储数据值。字典是有序*、可变且不允许重复的集合。(从 Python 3.7 版开始,字典是有序的。在 Python 3.6 及更早版本中,字典是无序的。)
9.1.1 创建字典与访问
字典是用大括号写的,有键和值。 创建并打印字典:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
字典项是有序的、可变的,并且不允许重复。字典项以键值对的形式呈现,可以使用键名进行引用。 例如打印brand的值
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
9.1.2 字典长度
还是用用len函数
hisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
9.1.3 数据类型
(1)字典项中的值可以是任何数据类型: 例如:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
类型:dict() (2)打印字典的数据类型:
thisdict = {
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
print(type(thisdict))
(3)与其他数据类型的区别 列表是有序且可变的,允许重复元素; 元组是有序且不可更改的,允许重复元素; 集合是无序且无索引的,无重复元素; 字典是有序且可变的,无重复元素。
9.2 字典的“增”
9.2.1 关键词索引添加
比如我要添加一个年龄为20:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=21
print(thisdict)
9.2.2 使用update
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=21
print(thisdict)
thisdict.update({'age':'21岁'})
print(thisdict)
9.3 字典的“删”
9.3.1 pop()方法
删除具有指定键名的项。 比如我要删除地址项目:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
print(thisdict)
thisdict.pop("address")
print(thisdict)
9.3.2 popitem()方法
删除最后插入的项目(在 3.7 之前的版本中,将删除随机项目):
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.pop("address")
print(thisdict)
thisdict.popitem()
print(thisdict)
9.3.3 del关键字
删除与指定键名称的项目:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
del thisdict['name']
print(thisdict)
也可以完全删除该字典。
9.3.4 clear()方法
清空字典:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.clear()
print(thisdict)
9.4 字典的“改”
9.4.1 关键词索引
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['name'] = 'hg菜鸟'
print(thisdict)
9.4.2 使用update()方法:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.update({'name':'hg菜鸟'})
print(thisdict)
9.5 字典的“查”
9.5.1 访问键名
(1)可以通过引用方括号内的键名来访问字典的项目:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
x = thisdict["name"]
print(x)
(2)还有一个被调用的方法get()会给你同样的结果:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
x = thisdict["name"]
y=thisdict.get('name')
print(x)
print(y)
9.5.2 访问键值
(1)keys()方法将返回字典中所有键的列表。
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
x = thisdict.keys()
print(x)
(2)向原始字典添加一个新项目,并看到键列表也得到更新:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=20
print(thisdict)
(3)获取值
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
x = thisdict.values()
print(x)
(4)items()方法将返回字典中的每个项目,作为列表中的元组。
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=20
print(thisdict)
x = thisdict.items()
print(x)
(5)要确定字典中是否存在指定的键,请使用in关键字:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
if 'name' in thisdict:
print('name在字典')
9.6 遍历字典
9.6.1 打印字典中的所有键名
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict:
print(x)
9.6.2 打印字典中的所有值:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict:
print(thisdict[x])
9.6.3 使用values()方法返回字典的值:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict.values():
print(x)
9.6.4 可以使用该keys()方法返回字典的键:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict.keys():
print(x)
9.6.5 使用以下方法循环遍历keys和valuesitems():
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x, y in thisdict.items():
print(x, y)
9.7 复制字典
9.7.1 用copy()函数
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
mydict=thisdict.copy()
print(mydict)
9.7.2 内置dict()函数
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
mydict=dict(thisdict)
print(mydict)
9.8 字典的嵌套
创建一个包含三个字典的字典:
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
print(myfamily)
创建三个字典,然后创建一个包含其他三个字典的字典:
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
print(myfamily)
练习题
Q1-使用get方法打印汽车字典的“model”键的值。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car.get("model"))
Q2-将“year”值从 1964 更改为 2020。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car["year"]=2020
print(car)
Q3-将键/值对 “color” : “red” 添加到汽车字典中。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car["color"]="red"
print(car)
Q4-使用 pop 方法从汽车字典中删除“model”。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop("model")
print(car)
Q5-使用clear方法清空car字典。
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)
answer都经过python调试。
-
https://chuanchuan.blog.csdn.net/article/details/120419754?spm=1001.2014.3001.5502 -
用Python玩转数据——中国大学mooc
|