Code
alien_0 = {"color":"green","points":5}
alien_0{'color': 'green', 'points': 5}
字典(dictionary)是一系列键值对。每个键相关联的值可以是数、字符串、列表、元组乃至字典。
alien_0 = {"color":"green","points":5}
alien_0{'color': 'green', 'points': 5}
alien_0["color"]
alien_0["points"]5
alien_0["x"] = 0
alien_0["y"] = 25
alien_0{'color': 'green', 'points': 5, 'x': 0, 'y': 25}
alien_0 = {}
alien_0["color"] = "green"
alien_0["points"] = 5
alien_0{'color': 'green', 'points': 5}
alien_0["color"] =[ "yellow","red","blue"]
alien_0{'color': ['yellow', 'red', 'blue'], 'points': 5}
alien_0 = {"color":"green","points":5}
del alien_0["points"]
alien_0{'color': 'green'}
like_language={
"jen":"python",
"sarah":"c",
"edward":"r",
"phil":"python",
}
like_language{'jen': 'python', 'sarah': 'c', 'edward': 'r', 'phil': 'python'}
get()方法访问值alien_0["speed"]
#>>> Traceback (most recent call last):
#>>> File "<string>", line 1, in <module>
#>>> KeyError: 'speed'get()方法第一个参数用于指定键key,第二个参数为指定的键不存在时的返回值。
alien_0 = {"color":"green","points":5}
alien_0.get("color")
alien_0.get("speed","Error,no existing")'Error,no existing'
Dict.items()
like_language={
"jen":"python",
"sarah":"c",
"edward":"r",
"phil":"python",
}
like_language.items()
for i,j in like_language.items():
print(f"Key:{i}")
print(f"Value:{j}")
print(f"{i.title()}'s favorite language is {j.title()}.\n")Key:jen
Value:python
Jen's favorite language is Python.
Key:sarah
Value:c
Sarah's favorite language is C.
Key:edward
Value:r
Edward's favorite language is R.
Key:phil
Value:python
Phil's favorite language is Python.
Dict.keys()
like_language.keys()
for i in like_language.keys():
print(f"Key:{i.title()}")Key:Jen
Key:Sarah
Key:Edward
Key:Phil
默认遍历使用键
for i in like_language:
print(f"Key:{i.title()}")Key:Jen
Key:Sarah
Key:Edward
Key:Phil
for i in like_language: print(f”Key:{i.title()}“)
sorted()函数
like_language={
"jen":"python",
"sarah":"c",
"edward":"r",
"phil":"python",
}
for i in sorted(like_language.keys()):
print(i.title())Edward
Jen
Phil
Sarah
Dict.values()
like_language.values()
for lan in like_language.values():
lan.title()为剔除重复项,可使用集合set()函数。
for lan in set(like_language.values()):
lan.title()集合的性质:互异性,无序性,确定性。
可以使用一对花括号{}直接创建集合,用逗号分隔。
language_set = {"python","r","c"}
language_set{'c', 'python', 'r'}
列表的每个元素是一个字典
alien_0 = {'color': 'green', 'points': 5}
alien_1 = {'color': 'yellow', 'points': 10}
alien_2 = {'color': 'red', 'points': 15}
aliens = [alien_0, alien_1, alien_2]
for alien in aliens:
print(alien){'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
r_object = {
"num":[1,2,3,4,5],
"strings":["qwe","asd","zxc"],
"bool":[True,False],
}
r_object["num"][1, 2, 3, 4, 5]
email = {
"Tencent":{
"prefix":"3036683293",
"suffix":"@qq.com",
"state":"hardly"
},
"USTC":{
"prefix":"wangal00",
"suffix":"@mail.ustc.edu.cn",
"state":"usually",
},
"Microsoft":{
"prefix":"wanganlin00",
"suffix":"@outlook.com",
"state":"often",
},
"Google":{
"prefix":"wanganlin00",
"suffix":"@gmail.com",
"state":"can't",
},
}
for i,j in email.items():
print(f"Belongs to {i}:")
print(f'My email is {j["prefix"]}{j["suffix"]} , I {j["state"]} use it.\n')Belongs to Tencent:
My email is 3036683293@qq.com , I hardly use it.
Belongs to USTC:
My email is wangal00@mail.ustc.edu.cn , I usually use it.
Belongs to Microsoft:
My email is wanganlin00@outlook.com , I often use it.
Belongs to Google:
My email is wanganlin00@gmail.com , I can't use it.