6-7?人:在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都
存储在一个名为people的列表中。遍历这个列表,将其中每个人的所有信息都打印出来。
marx={'first_name':'karl','last_name':'marx','age':'103','city':'landon'}
einstein={'first_name':'albert','last_name':'einstein','age':'142','city':'American'}
maozedon={'first_name':'mao','last_name':'zedon','age':'128','city':'hunan'}
people=[marx,einstein,maozedon]
print(people[0])
print(people[1])
print(people[2])
(用for遍历更为方便)?
?运行结果:
?6-8?宠物:创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
Tom={'type':'cat','monster':'awesome'}
Jerry={'type':'mouse','monster':'xinxin'}
Donald={'type':'duck','monster':'baby'}
pets=[Tom,Jerry,Donald]
for pet in pets:
print(pet)
运行结果:
?
?6-9?喜欢的地方:创建一个名为favorite_places的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
favorite_places={'baoyusong':['hanzhong','daughter city','jiangxi'],
'awesome':['shanghai'],
'xin':['paris','shanghai']}
for k,v in favorite_places.items():
if len(v)==1:
print(k.title()+"'s favorite place is: ")
else:
print(k.title()+"'s favorite places are: ")
#遍历字典中的键对应的值
for place in v :
print('\t'+place.title())
运行结果:
?
?6-11?城市:创建一个名为cities的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country、population和fact等键。将每座城市的名字以及有关它们的信息都打印出来。
?
?(英文有点长了😅)
cities={'shanghai':{'belong':'china','population':'2487.09w','fact':'Four distinct seasons, sufficient sunshine, abundant rainfall, mild and humid climate, shorter spring and autumn, longer winter and summer'},
'beijing':{'belong':'china','population':'2189.31w','fact':'Warm temperate zone, semi-humid and semi-arid monsoon climate, hot and rainy in summer, cold and dry in winter, short spring and autumn'},
'jiangxi':{'belong':'china','population':'4518.86w','fact':'The terrain is high in the south and low in the north, which is conducive to water gathering, dense water network and abundant precipitation'}
}
for city,info in cities.items():
print('\n'+city.title()+':')
#将china首字母大写
info['belong']='China'
for k,v in info.items():
print('\t'+k.title()+":"+v)
?优化一下:
cities={'shanghai':{'belong':'china','population':'2487.09w','fact':'Four distinct seasons, sufficient sunshine, abundant rainfall, mild and humid climate, shorter spring and autumn, longer winter and summer'},
'beijing':{'belong':'china','population':'2189,31w','fact':'Warm temperate zone, semi-humid and semi-arid monsoon climate, hot and rainy in summer, cold and dry in winter, short spring and autumn'},
'jiangxi':{'belong':'china','population':'4518.86w','fact':'The terrain is high in the south and low in the north, which is conducive to water gathering, dense water network and abundant precipitation'}
}
for city,info in cities.items():
print('\n'+city.title()+':')
for k,v in info.items():
#将china首字母大写
if v == "china":
print('\t'+k.title()+":"+v.title())
else:
print('\t'+k.title()+":"+v)
再优化一下:
cities={'shanghai':{'belong':'china','population':'2487.09w','fact':'Four distinct seasons, sufficient sunshine, abundant rainfall, mild and humid climate, shorter spring and autumn, longer winter and summer'},
'beijing':{'belong':'china','population':'2189.31w','fact':'Warm temperate zone, semi-humid and semi-arid monsoon climate, hot and rainy in summer, cold and dry in winter, short spring and autumn'},
'jiangxi':{'belong':'china','population':'4518.86w','fact':'The terrain is high in the south and low in the north, which is conducive to water gathering, dense water network and abundant precipitation'}
}
for city,info in cities.items():
print('\n'+city.title()+':')
#将china首字母大写
for k,v in info.items():
print('\t'+k.title()+":"+v.title() if v=='china' else v)
运行结果都是一样的:
|