基本形式:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
字典用于在键值对中存储数据值。字典是有序*、可变且不允许重复的集合。(从 Python 3.7 版开始,字典是有序的。在 Python 3.6 及更早版本中,字典是无序的。)
字典是用大括号写的,有键和值。
创建并打印字典:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
字典项是有序的、可变的,并且不允许重复。字典项以键值对的形式呈现,可以使用键名进行引用。
例如打印brand的值
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
还是用用len函数
hisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
(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)与其他数据类型的区别
列表是有序且可变的,允许重复元素;
元组是有序且不可更改的,允许重复元素;
集合是无序且无索引的,无重复元素;
字典是有序且可变的,无重复元素。
比如我要添加一个年龄为20:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=21
print(thisdict)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['age']=21
print(thisdict)
thisdict.update({'age':'21岁'})
print(thisdict)
删除具有指定键名的项。
比如我要删除地址项目:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
print(thisdict)
thisdict.pop("address")
print(thisdict)
删除最后插入的项目(在 3.7 之前的版本中,将删除随机项目):
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.pop("address")
print(thisdict)
thisdict.popitem()
print(thisdict)
删除与指定键名称的项目:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
del thisdict['name']
print(thisdict)
也可以完全删除该字典。
清空字典:
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.clear()
print(thisdict)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict['name'] = 'hg菜鸟'
print(thisdict)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
thisdict.update({'name':'hg菜鸟'})
print(thisdict)
(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)
(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在字典')
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict:
print(x)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict:
print(thisdict[x])
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict.values():
print(x)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x in thisdict.keys():
print(x)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
for x, y in thisdict.items():
print(x, y)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
mydict=thisdict.copy()
print(mydict)
thisdict = {
"name": "hg",
"address": "长沙",
"year": 2000
}
mydict=dict(thisdict)
print(mydict)
创建一个包含三个字典的字典:
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