date 2018年12月29日
#前面学习的字典只能存储一个外星人的信息,如果是好多外星人呢?
#或者说一个字典,存储某一类信息,如果要存储好多类信息呢?这时候需要我们用列表来存储字典
#例如下面的程序
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)
#当然这是很一般的情形,本质上一个字典也可以实现
#下面我们来看更符合实际的一种情况
result
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}
#用range()函数生成外星人的代号0-29(注意range()是从0开始的)
aliens = [] #let us creat an empty list first
for alien_nummber in range(30):
new_alien = {'color ': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien) # fill new_alien in aliens *append is '附件' in Chinese
#every aliens has the same characteristic
#try to show the top 5 aliens
for alien in aliens[: 5]:
print(alien)
print('...')
#show the number of aliens you have created
number = len(aliens)
print("The number of aliens: " + str(number))
result
{'color ': 'green', 'points': 5, 'speed': 'slow'}
{'color ': 'green', 'points': 5, 'speed': 'slow'}
{'color ': 'green', 'points': 5, 'speed': 'slow'}
{'color ': 'green', 'points': 5, 'speed': 'slow'}
{'color ': 'green', 'points': 5, 'speed': 'slow'}
...
The number of aliens: 30
#try to modify the top 3 aliens characteristic
aliens = [] #same example above
for alien_nummber in range(0,30):
new_alien = {'color': 'green', 'speed': 'slow', 'points': 5}
aliens.append(new_alien)
#Now let us modify...
for alien in aliens[0 : 3]:
if alien['color'] is 'green':
alien['color'] = 'yellow'
alien['speed'] = 'medium'
alien['points'] = 10
#print the top 5 aliens and detect the change
for alien in aliens[0 : 5]:
print(alien)
print('...')
#Many inexplicable error! such as the 'space','the colon' is different in Chinese and English
result
{'color': 'yellow', 'speed': 'medium', 'points': 10}
{'color': 'yellow', 'speed': 'medium', 'points': 10}
{'color': 'yellow', 'speed': 'medium', 'points': 10}
{'color': 'green', 'speed': 'slow', 'points': 5}
{'color': 'green', 'speed': 'slow', 'points': 5}
...
#On the contrary,how to store lists in a dictionary?
#Let's see the following example
#Information about Pizza
pizza = {
'crust': 'thick', #crust means '壳',topping means '配料'
'toppings': ['mushrooms', 'extra cheese'] #store a list in a dictionary
}
print("You ordered a " + pizza['crust'] + "-crust pizza" + "with the following toppings:")
for topping in pizza['toppings']:
print("\t" + topping)
result
You ordered a thick-crust pizzawith the following toppings:
mushrooms
extra cheese
#Count up students' scores
grade = {
'Shi shang':['Math - 98', 'Chinese - 96'], #Bear in mind this comma
'Ding shiyu':['Math - 97','Chinese - 95'], #comma means '逗号'
'Lv yuetong':['Math - 100','Chinese - 98']
}
for name, scores in grade.items():
print("\n" + name.title() + "'s final exam grades are:")
for score in scores:
print("\t" + score)
result
Shi Shang's final exam grades are:
Math - 98
Chinese - 96Ding Shiyu's final exam grades are:
Math - 97
Chinese - 95Lv Yuetong's final exam grades are:
Math - 100
Chinese - 98
#Use a dictionary in dictionary
#the same example above
grade = {
'math' : {'Shishang': 98, 'Lvyuetong': 100, 'Dingshiyu': 97},
'chinese' : {'Shishang': 96, 'Lvyuetong': 98, 'Dingshiyu': 97}
} # Notice the form of the definition above
for subjects, scores in grade.items():
print(subjects)
print(scores)
result
math
{'Shishang': 98, 'Lvyuetong': 100, 'Dingshiyu': 97}
chinese
{'Shishang': 96, 'Lvyuetong': 98, 'Dingshiyu': 97}

网友评论