<1>什么是字典?
字典是由一系列的键-值对组成。在python中,字典用{}表示,列表用【】表示,元组用()表示。
<2>字典的基本操作
字典的操作,包括字典的定义、添加键值对、删除键值对、遍历键值对、分别遍历key/value值
2.1 定义字典及使用它
# 1.define dictionary ,and use it
dictionary1={'color':'green', 'width':15, 'height':20}
print(dictionary1)
print(dictionary1['color'])
print(dictionary1['width'])
结果:
{'color': 'green', 'width': 15, 'height': 20}
green
15
2.2 创建一个空字典,并添加元素(键值对)
# 2.creat an empty dictionary,and add key-value pairs
dictionary2={}
dictionary2['x']=15
dictionary2['y']=20
print(dictionary2)
结果:
{'x': 15, 'y': 20}
2.3 修改键值对
# 3.modify the key-value
dictionary2['x']=30
dictionary2['y']=50
print('New value:'+' x:'+str(dictionary2['x'] )+' y:'+str(dictionary2['y'] ))
结果:
New value: x:30 y:50
2.4 删除键值对
# 4.delete the key-value
del dictionary2['x']
print(dictionary2)
结果:
{'y': 50}
2.5 遍历键值对
首先,是遍历字典中所有键值对。
# 5.traverse all the key-value
dictionary3={'Mike':'C','Jerry':'java','Lily':'python','Gogo':'c++'}
for key,value in dictionary3.items():
print(key+':'+value.title())
print('\n')
结果:
Mike:C
Jerry:Java
Lily:Python
Gogo:C++
2.6 仅遍历所有键key
首先,是按字典顺序遍历所有key值
# 6. traverse all the keys
for key in dictionary3.keys():
print(key)
print('\n')
结果:
Mike
Jerry
Lily
Gogo
其次,按照字母排序顺序遍历所有key值
# sorted the dictionary.key
for key in sorted(dictionary3.keys() ):
print(key)
print('\n')
结果:
Gogo
Jerry
Lily
Mike
2.7 遍历所有value值
# 7.traverse all the values
for value in dictionary3.values():
print(value.title())
print('\n')
结果:
C
Java
Python
C++
3.嵌套
在这一部分,我们将学习较复杂的字典操作----嵌套。包含列表中嵌套字典,字典中嵌套列表,字典中嵌套字典。
3.1 列表中嵌套字典
例如,机器人aliens中包含{alien_0,alien_1,alien_2}。
# 嵌套 Nested
# 1.dictionaries are nested
alien_0={'color':'green','point':5}
alien_1={'color':'red','point':15}
alien_2={'color':'yellow','point':25}
# 2. dictionaries are stored in the list
aliens=[alien_0,alien_1,alien_2]
print(aliens)
for alien in aliens[0:3]:
if alien['color']=='green':
alien['color']='black'
elif alien['color']=='red':
alien['color']='purple'
print(aliens)
print('\n')
结果:
[{'color': 'green', 'point': 5}, {'color': 'red', 'point': 15}, {'color': 'yellow', 'point': 25}]
[{'color': 'black', 'point': 5}, {'color': 'purple', 'point': 15}, {'color': 'yellow', 'point': 25}]
3.2 字典中嵌套列表
例如,字典中包含多个人,每个人有多种擅长的语音。
# 3.lists are stored in the dictionary
dictionary4={
'mike':['c','python'],
'lily':['java','c++','matlab'],
'tom':['java','python']
}
for name,languages in dictionary4.items():
print(name.title()+':'+str(len(languages)))
for language in languages:
print(language.title())
print('\n')
结果:
Mike:2
C
Python
Lily:3
Java
C++
Matlab
Tom:2
Java
Python
3.3 字典中嵌套字典
例如,字典中包含多个人,每个人有多种属性(如喜欢的食物、擅长的语言、喜欢的活动)。即key--对应着人,value--对应着多种属性。
# 4. dictionaries are stored in dictionaries
dictionary5={
'Mike':{
'food':['water','bread'],
'language':['c','python'],
'sport':['run','jump'],
},
'Tom':{
'food':['meat','rice'],
'language':['java','matlab'],
'sport':['walk','jump'],
},
}
for name,likess in dictionary5.items():
print(name+':') # Mike
for likeCata,likes in likess.items(): # like
print(likeCata+':') # food
for like in likes:
print(like)
print('\n')
结果:
Mike:
food:
water
bread
language:
c
python
sport:
run
jump
Tom:
food:
meat
rice
language:
java
matlab
sport:
walk
jump
注意:遍历字典时,要用.items()。