示例:
alien_0 = {'color':'green','points':5}
print(alien_0['color'])
print(alien_0['points'])
green
5
1、使用字典:
-
字典:是一系列键—值对。(每个键都与一个值相关联,可以使用键来访问与之相关联的值)
-
字典用放在花括号{ }中的一系列键——值对表示,键和值之间用冒号分隔,而键—值对之间用用逗号分隔。
alien_0 = {'color':'green','points':5}
-
访问字典中的值:
- 要获取与键相关联的值,可依次指定字典名和放在方括号内的键:
alien_0 = {'color':'green'} print(alien_0['color'])
-
添加键—值对:
- 添加键-值对,可依次指定字典名、用方括号括起的键和相关联的值。
alien_0 = {'color':'green','points':5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
{'color':'green','points':5}
{'color':'green','points':5,'x_position':0,'y_position':25}
-
修改、删除字典中的值:
- 修改字典中的值,可依次指定字典名、用方括号括起键以及与该键相关联的新值。
- 删除键值对,可使用del语句;使用del语句时,必须指定字典名和要删除的键。
alien_0 = {'color':'green'} print("The alien is " + alien_0['color'] + ".") alien_0['color'] = 'yellow' print(""The alien is now " + alien_0['color'] + "."") The alien is green. The alien is now yellow. alien_0 = {'color':'green','points':5} print(alien_0) del alien_0['points'] print(alien_0) {'color':'green','points':5} {'color':'green'}
2、遍历字典:
-
用于遍历字典的for循环,可声明两个变量(可使用任何名称),用于存储键-值对中的键和值。
favorite_languages = { 'jen':'python', 'sarah':'c', 'edward':'ruby', 'phil':'python' } for name,language in favoriute_languages.items(): print(name.title() + "'s favorite language is " + language.title() + ".") Jen's favorite language is Python. Sarah's favorite language is C. Edward's favorite language is Ruby. Phil's favorite language is Python.
-
遍历字典中的所有键:
-
不需要使用字典中的值时,可以使用方法key( )【可省略】
favorite_languages = { 'jen':'python', 'sarah':'c', 'edward':'ruby', 'phil':'python' } for name in favorite_languages.keys(): print(name.title())
-
方法keys( )并非只能用于遍历;实际上,它返回一个列表,其中包括字典中的所有键。
favorite_languages = { 'jen':'python', 'sarah':'c', 'edward':'ruby', 'phil':'python' } for 'erin' not in favorite_languages.keys(): print("Erin,please take our poll!") Erin,please take our poll!
-
按顺序遍历字典中的所有键:
要以特定的顺序返回元素,一种办法是在for循环中对返回的键进行排序。为此,可使用**函数sorted()**来获得按特定顺序排列的键列表的副本
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.") Edward,thank you for taking the poll. Jen,thank you for taking the poll. Pjill,thank you for taking the poll. Sarah,thank you for taking the poll.
-
-
遍历字典中的所有值:
方法values(),它返回一个值列表,而不包含任何键。
**集合函数set()**可剔除重复项
favorite_languages = { 'jen':'python', 'sarah':'c', 'edward':'ruby', 'phil':'python' } print("The following languages have been mentioned:") for language in set(favorite_languages.values()): print(language.title()) Python C Ruby
3、嵌套:
-
字典列表:
#创建一个用于存储外星人的空列表 aliens = [] #创建30个绿色的外星人 for alien_number in range(0,30): new_alien = {'color':'green', 'point':5, 'speed':'slow'} aliens.append(new_alien) for alien in aliens[0:3]: if alien['color'] == 'green': alien['color'] = 'yellow' alien['speed'] = 'medium' alien['point'] = 10 for alien in aliens[0:5]: print(alien) print("...")
-
在字典中存储列表:
favorite_languages = { 'jen' : ['python','ruby'], 'sarah' : ['c'], 'edward' : ['ruby','go'], 'phil': ['python','haskell'], } for name,languages in favorite_languages.items(): print("\n" + name.title() + "'s favorite language are:") for language in languages: print("\t" + language.title()) Jen's favorite languages are: Python Ruby Sarah's favorite languages are: C Phil's favorite languages are: Python Haskell Edward's favorite language are: Ruby Go
-
在字典中存储字典:
user = { '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['last'] location = user_info['location'] print("\tFull name: " + full_name.title()) print("\tLocation: " + location.title()) Username:aeinstein Full name: Albert Einstein Location:Princeton Username:mcurie Full name: Marie Curie Location:Paris