#第六章 字典######
1、Python中字典是放在{}中的一系列键-值对,一个键关联一个值,使用键来访问对应的值,
2、字典添加键值对(可定义空字典进行添加)
3、修改字典的值(用中括号括起来的键和与该键关联的新值)
4、删除字典的键值对,del语句
5、由类似对象组成的字典
6、遍历字典(遍历所有的键值对items()方法,遍历所有的键keys()方法,遍历所有的值values()方法)
6.1 遍历所有的键值对items()方法
6.2 遍历字典所有的键keys()方法
6.3 遍历所有的值values()方法
7、嵌套,字典嵌套列表,列表嵌套字典或者字典嵌套字典
7.1 字典嵌套在列表中
7.2 字典中存储列表,字典一个键关联多个值
7.3 字典中嵌套字典
1、Python中字典是放在{}中的一系列键-值对,一个键关联一个值,使用键来访问对应的值,
# 定义格式{‘键(key)’:‘值(value)’}和访问字典的值
alien = {'color':'green','points':5}
print("字典中的第一个值为: " + alien['color'])
print("字典中的第二个值为: " + str(alien['points']))
#输出结果:
字典中的第一个值为: green
字典中的第二个值为: 5
###################
2、字典添加键值对(可定义空字典进行添加)
alien = {'color':'green','points':5}
alien['x_position'] = 0 #添加X轴坐标
alien['y_position'] = 25 #添加y轴坐标
print(alien)
#输出结果:
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
###################
3、修改字典的值(用中括号括起来的键和与该键关联的新值)
alien = {'color':'green','points':5}
print("the color is "+ alien['color'])
alien['color'] = 'yellow' #修改key对应的值
print ("the new color is " + alien['color'])
#输出结果:
the color is green
the new color is yellow
###################
4、删除字典的键值对,del语句
alien = {'color':'green','points':5}
del alien['points'] #指定删除值对应的key
print (alien)
#输出结果:
{'color': 'green'}
###################
5、由类似对象组成的字典
favorite_languages = {
'jen':'python',
'sarah':'C',
'alien':'java',
'john':'python',
}
print ("jen's favorite language is "+favorite_languages['jen'].title() + ".")
#输出结果:
jen's favorite language is Python.
###################
6、遍历字典(遍历所有的键值对items()方法,遍历所有的键keys()方法,遍历所有的值values()方法)
6.1 遍历所有的键值对items()方法
user = {
'username':'efermi',
'first':'enrico',
'last':'fermi',
}
for key,value in user.items(): #for循环依次将每个键值对存储到指定的两个变量中
print("\n The Key-Value is " + key + " - " + value + '.')
#输出结果:
The Key-Value is username - efermi.
The Key-Value is first - enrico.
The Key-Value is last - fermi.
6.2 遍历字典所有的键keys()方法
favorite_languages = {
'jen':'python',
'sarah':'C',
'alien':'java',
'john':'python',
}
for name in favorite_languages.keys(): #keys()遍历所有的键
print(name.title())
for name in favorite_languages: #不带keys()也是遍历所有的键
print("No keys() back: " + name.title())
for name in sorted(favorite_languages.keys()): #对遍历结果进行排序sorted()
print("after sorted : " + name.title())
#输出结果:
Jen
Sarah
Alien
John
No keys() back: Jen
No keys() back: Sarah
No keys() back: Alien
No keys() back: John
after sorted : Alien
after sorted : Jen
after sorted : John
after sorted : Sarah
6.3 遍历所有的值values()方法
favorite_languages = {
'jen':'python',
'sarah':'C',
'alien':'java',
'john':'python',
}
for name in favorite_languages.values(): #values()遍历所有的值
print(name.title())
#输出结果:
Python
C
Java
Python
#去掉重复项,可使用集合(set)
favorite_languages = {
'jen':'python',
'sarah':'C',
'alien':'java',
'john':'python',
}
for name in set(favorite_languages.values()): #set()去掉重复值
print(name.title())
#输出结果:
Python
C
Java
###################
7、嵌套,字典嵌套列表,列表嵌套字典或者字典嵌套字典
7.1 字典嵌套在列表中
aliens = []
for alien_num in range(30): #创建30个绿色的外星人
new_alien = {'color':'green','point':5}
aliens.append(new_alien)
for alien in aliens[:5]: #显示前5个外星人
print(alien)
print("\n Total number od aliens: "+ str(len(aliens))) #显示创建的总个数
#输出结果:
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5}
{'color': 'green', 'point': 5}
Total number od aliens: 30
7.2 字典中存储列表,字典一个键关联多个值
favorite_languages = { #都以列表的方式存储值
'jen':['python','java'],
'sarah':['C','python'],
'alien':['java'],
'john':['python','c++'],
}
for name,languages in favorite_languages.items(): #获取键值对
for language in languages: #获取列表中的值
print(name.title() + "'s favorite languages is " + language)
#输出结果:
Jen's favorite languages is python
Jen's favorite languages is java
Sarah's favorite languages is C
Sarah's favorite languages is python
Alien's favorite languages is java
John's favorite languages is python
John's favorite languages is c++
7.3 字典中嵌套字典
users = {
'ae':{'first':'albert',
'last':'einstein',
'location':'princeton',},
'mu':{'first':'marie',
'last':'curie',
'location':'paris',},
}
for username,userinfo in users.items(): #for循环依次将每个键值对存储到指定的两个变量中
print("UserName is: " + username)
full_name = userinfo['first'] + " " + userinfo['last'] #获取嵌套字典中的值
localtion = userinfo['location']
print("The full_name is: " + full_name)
print("The localtion is: "+ localtion)
#输出结果:
UserName is: ae
The full_name is: albert einstein
The localtion is: princeton
UserName is: mu
The full_name is: marie curie
The localtion is: paris
Python编程从入门到实践基础知识:https://blog.csdn.net/louzhu_lz/article/details/90721685
Python编程从入门到实践(第三、四章的列表和元祖):https://blog.csdn.net/louzhu_lz/article/details/91354506
Python编程从入门到实践(第五章if语句学习总结):https://blog.csdn.net/louzhu_lz/article/details/91409903
Python编程从入门到实践(第六章字典学习总结):https://blog.csdn.net/louzhu_lz/article/details/91910554
Python编程从入门到实践(第七章用户输入和while循环学习总结):https://blog.csdn.net/louzhu_lz/article/details/92384649
Python编程从入门到实践(第八章函数)学习总结:https://blog.csdn.net/louzhu_lz/article/details/93377817