Python Dictionaries
- Previous Page Python Sets
- Next Page Python If Else
字典(Dictionary)
字典是一个无序、可变和有索引的集合。在 Python 中,字典用花括号编写,拥有键和值。
Hanyar
创建并打印字典:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } print(thisdict)
访问项目
您可以通过在方括号内引用其键名来访问字典的项目:
Hanyar
获取 "model" 键的值:
x = thisdict["model"]
还有一个名为 get()
的方法会给你相同的结果:
Hanyar
获取 "model" 键的值:
x = thisdict.get("model")
更改值
您可以通过引用其键名来更改特定项的值:
Hanyar
把 "year" 改为 2019:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["year"] = 2019
遍历字典
您可以使用 for
循环遍历字典。
循环遍历字典时,返回值是字典的键,但也有返回值的方法。
Hanyar
逐个打印字典中的所有键名:
for x in thisdict: print(x)
Hanyar
逐个打印字典中的所有值:
for x in thisdict: print(thisdict[x])
Hanyar
您还可以使用 values()
函数返回字典的值:
for x in thisdict.values(): print(x)
Hanyar
通过使用 items() 函数遍历键和值:
for x, y in thisdict.items(): print(x, y)
检查键是否存在
要确定字典中是否存在指定的键,请使用 in
关键字:
Hanyar
检查字典中是否存在 "model":
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } if "model" in thisdict: print("Yes, 'model' is one of the keys in the thisdict dictionary")
添加项目
通过使用新的索引键并为其赋值,可以将项目添加到字典中:
Hanyar
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["color"] = "red" print(thisdict)
删除项目
有几种方法可以从字典中删除项目:
Hanyar
pop() 方法删除具有指定键名的项:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict.pop("model") print(thisdict)
Hanyar
popitem()
Rarrabawa kawun kaiyanci na ya na baiyanci na kaiyanci (a hanyanci 3.7 kaiyanci):
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict.popitem() print(thisdict)
Hanyar
del thisdict["model"]
thisdict.popitem()
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } Kalmomin na iya sauransu wani abin da ke da kalmomin: print(thisdict)
Hanyar
del thisdict["model"]
del
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } Kalmomin na iya sauransu yankin gina: del thisdict
Hanyar
clear()
print(thisdict) #this akan yana da rauni, saboda "thisdict" ba a samu shi ne.
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } Kalmomin na iya yin sauransu yankin gina: print(thisdict)
thisdict.clear()
A ka iya sauransu yankin gina Ba za ta iya sauransu ta hanyar shirin
dict2 = dict1dict2
daga cikin dict1
zai iya yin sauransu, saboda dict1
wanda a yi sauransu kuma dict2
daga cikin.
Wannan na iya kasancewa wani lokaci na amfani da shiri na yankin gina: copy()
。
Hanyar
A amfani da copy()
Yana tsara yankin gina:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = thisdict.copy() print(mydict)
Wani lokaci na amfani da shiri na yankin gina domin yana tsara yankin gina shine amfani da shiri na yankin gina: dict()
。
Hanyar
A amfani da dict()
Yana tsara yankin gina na yankin gina:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = dict(thisdict) print(mydict)
Yankin gina na yankin gina
Tasirin yankin gina na iya kasancewa takwas na yankin gina, wanda a kira yankin gina na yankin gina.
Hanyar
A tsara yankin gina kan takwas na yankin gina:
myfamily = { "child1" : { "name" : "Phoebe Adele", "year" : 2002 }, "child2" : { "name" : "Jennifer Katharine", "year" : 1996 }, "child3" : { "name" : "Rory John", "year" : 1999 } }
ama idan ba ka fẹrinsa ya fi yi sauransu tare da takwas na yankin gina da suka kasance yankin gina:
Hanyar
A tsara takwas na yankin gina, kuma yana tsara yankin gina kan takwas na yankin gina:
child1 = { "name" : "Phoebe Adele", "year" : 2002 } child2 = { "name" : "Jennifer Katharine", "year" : 1996 } child3 = { "name" : "Rory John", "year" : 1999 } myfamily = { "child1" : child1, "child2" : child2, "child3" : child3 }
dict() aikin yankin gina
An amfani da dict()
Yana tsara aikin yankin gina nau'iwar dict:
Hanyar
thisdict = dict(brand="Porsche", model="911", year=1963) # Ka da farko, kalmomin na iya kasancewa kama a cikin hadadda na tsintsi # Ka da farko, a na yi wani kuma ya fi hanyar idanin yana gudanarwa da idanin print(thisdict)
Dictionary Methods
Python provides a set of built-in methods that can be used on dictionaries.
Method | Description |
---|---|
clear() | Remove all elements from the dictionary |
copy() | Return a copy of the dictionary |
fromkeys() | Return a dictionary with the specified key and value |
get() | Return the value of the specified key |
items() | Return a list of tuples containing each key-value pair |
keys() | Return a list containing all the keys of the dictionary |
pop() | Remove the element with the specified key |
popitem() | Remove the last inserted key-value pair |
setdefault() | Return the value of the specified key. If the key does not exist, insert a key with the specified value. |
update() | Update the dictionary with the specified key-value pair |
values() | Return a list of all values in the dictionary |
- Previous Page Python Sets
- Next Page Python If Else