Python字典
字典
字典是无序,可更改和索引的集合。在Python中,字典用大括号括起来,并且具有键和值。
访问项目
您可以通过在方括号内引用其键名来访问字典的各项:
还有一种称为的方法get()
,将为您提供相同的结果:
变更值
您可以通过参考特定项的键名来更改其值:
例
将“年份”更改为2018:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["year"] = 2018
自己尝试»
遍历字典
您可以使用for
循环来循环浏览字典
。
当遍历字典时,返回值是字典的键,但是也有一些方法可以返回值。
检查密钥是否存在
要确定字典中是否存在指定的键,请使用in
关键字:
例
检查字典中是否存在“模型”:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is
one of the keys in the thisdict dictionary")
自己尝试»
字典长度
要确定字典中有多少项(键值对),请使用len()
函数。
新增项目
通过使用新的索引键并为其分配值,可以向字典中添加项目:
例
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
自己尝试»
移除物品
有几种方法可以从字典中删除项目:
例
该pop()
方法删除具有指定键名的项目:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
自己尝试»
例
该popitem()
方法删除最后插入的项(在3.7之前的版本中,将删除随机项):
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.popitem()
print(thisdict)
自己尝试»
例
该del
关键字删除与指定键名称的项目:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
自己尝试»
例
该del
关键字也可以删除字典完全:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict"
no longer exists.
自己尝试»
例
该clear()
方法清空字典:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
自己尝试»
复制字典
您不能复制字典只需通过打字dict2 =
dict1
,这是因为:dict2
将只能是一个
参考来dict1
,和所做的更改
dict1
会自动也在进行
dict2
。
有很多方法可以制作副本,一种方法是使用内置的Dictionary方法
copy()
。
例
使用以下copy()
方法复制字典:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict
= thisdict.copy()
print(mydict)
自己尝试»
制作副本的另一种方法是使用内置函数
dict()
。
例
使用以下dict()
功能复制字典:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict
= dict(thisdict)
print(mydict)
自己尝试»
嵌套词典
词典也可以包含许多词典,这被称为嵌套词典。
例
创建一个包含三个词典的字典:
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
自己尝试»
或者,如果您想嵌套三个已经作为字典存在的字典:
例
创建三个字典,然后创建一个包含其他三个字典的字典:
child1 = {
"name" : "Emil",
"year" : 2004
}
child2 = {
"name" : "Tobias",
"year" : 2007
}
child3 = {
"name" : "Linus",
"year" : 2011
}
myfamily = {
"child1" : child1,
"child2" : child2,
"child3" : child3
}
自己尝试»
dict()构造函数
也可以使用dict()构造函数创建一个新字典:
例
thisdict = dict(brand="Ford", model="Mustang", year=1964)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)
自己尝试»
字典方法
Python具有一组可用于词典的内置方法。
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key value pair |
keys() | Returns a list containing the dictionary's keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |