多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ***** # 6.1 一个简单的字典 像json对象,键值对 ``` alien_0 = {'color': 'green', 'points': 5} print(alien_0['color']) ``` # 6.2 使用字典 字典是一系列键—值对。每个键都与一个值相关联,你可以使用键来访问与之 相关联的值。与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将任何Python对象用作字典中的值。 ``` alien_0 = {'color': 'green', 'points': 5} ``` ## 6.2.1 访问字典中的值 ``` alien_0 = {'color': 'green'} print(alien_0['color']) # green ``` ## 6.2.2 添加键—值对 字典是一种动态结构,可随时在其中添加键—值对。要添加键—值对,可依次指定字典名、用方括号括起的键和相关联的值。 ``` alien_0 = {'color': 'green', 'points': 5} print(alien_0) alien_0['x_position'] = 0 alien_0['y_position'] = 25 #{'color': 'green', 'points': 5} #{'color': 'green', 'points': 5, 'y\_position': 25, 'x\_position': 0} ``` ## 6.2.3 先创建一个空字典 ``` alien_0 = {} alien_0['color'] = 'green' alien_0['points'] = 5 ``` ## 6.2.4 修改字典中的值 ``` alien_0 = {'color': 'green'} print("The alien is " + alien_0['color'] + ".") alien_0\['color'] = 'yellow' ``` ## 6.2.5 删除键— 值对 ``` alien_0 = {'color': 'green', 'points': 5} print(alien_0) del alien_0['points'] ``` ## 6.2.6 由类似对象组成的字典 要调查很多人,询问他们最喜欢的编程语言,可使用一个字典来存储这种简单调查的结果,如下所示: ``` favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } ``` # 6.3 遍历字典 ## 6.3.1 遍历所有的键— 值对 ``` user_0 = {'username': 'efermi', 'first': 'enrico', 'last': 'fermi', } for key, value in user_0.items(): print("\nKey: " + key) print("Value: " + value) ``` ## 6.3.2 遍历字典中的所有键 ``` favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for name in favorite_languages.keys(): ``` ## 6.3.3 按顺序遍历字典中的所有键 ``` favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for name in sorted(favorite_languages.keys()): print(name.title() + ", thank you for taking the poll.") ``` ## 6.3.4 遍历字典中的所有值 ``` favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for language in favorite_languages.values(): print(language.title()) ``` **将字典中不重复的值都取出来,使用集合set** ``` favorite_languages = { 'jen': 'python', 'sarah': 'c', 'edward': 'ruby', 'phil': 'python', } for language in set(favorite_languages.values()): print(language.title()) ``` # 6.4 嵌套 将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。你 可以在列表中嵌套字典、在字典中嵌套列表甚至在字典中嵌套字典。 ## 6.4.1 字典列表 将字典嵌套到列表中 创建一个外星人列表,其中每个外星人都是一个字典,包含有关该外星人的各种信息。 ``` 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) ``` 创建30个外星人 ``` aliens = [] # 创建30个绿色的外星人 # 循环30次 for alien_number in range(30): new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'} aliens.append(new_alien) ``` ## 6.4.2 在字典中存储列表 ``` pizza = { 'crust': 'thick', 'toppings': ['mushrooms', 'extra cheese'], } for topping in pizza['toppings']: print("\t" + topping) ``` ## 6.4.3 在字典中存储字典 可在字典中嵌套字典。例如,如果有多个网站用户, 每个都有独特的用户名,可在字典中将用户名作为键,然后将每位用户的信息存储在一个字典中,并将该字典作为与用户名相关联的值。在下面的程序中,对于每位用户,我们都存储了其三项信息:名、姓和居住地 ``` #在字典中存储字典 users = {'aeinstein': {'first': 'albert', 'last': 'einstein', 'location': 'princeton'}, 'mcurie': {'first': 'marie', 'last': 'curie', 'location': 'paris'}, } #对字典进行循环 for username, user_info in users.items(): print("\nUsername: " + username) full_name = user_info['first'] + " " + user_info['last'] location = user_info['location'] print("\tFull name: " + full_name.title()) print("\tLocation: " + location.title()) ```