#练习 6-1:
messages_man = {
'first name':'宝宝',
'last name':'海绵',
'age':'35',
'city':'比奇堡海滩'
}
for key,value in messages_man.items():
print(f'key:{key}')
print(f'value:{value}\n')
#练习 6-2:
favorite_digital = {'yzj':'7',
'lj':'7',
'ltt':'8',
'cqa':'7',
'nwh':'0',
}
for name,digital in favorite_digital.items():
print(f'{name.title()}最喜欢的数字是{digital}\n')
#练习 6-3:
terms = {
'if-else':'条件语句',
'input':'输入语句',
'print':'输出语句',
'for':'遍历循环语句',
'while':'判断循环语句',
}
for term,meaning in terms.items():
print(f'{term}:{meaning}\n')
#练习 6-4:
# i got it,i mean there is no need to do it
#练习 6-5:
rivers = {'nile':'egypt',
'yellow':'china',
'amazon':'brazil'
}
for river,country in rivers.items():
print(f'The {river.title()} runs through {country.title()}\n')
for river in rivers:
print(river.title())
for country in rivers.values():
print(country.title())
#练习 6-6:
favorite_digital = {'yzj':'7',
'lj':'7',
'ltt':'8',
'cqa':'7',
'nwh':'0',
}
invested_man = ['yzj','nwh','ly']
for man in invested_man:
if man in favorite_digital:
print(f'{man.title()},谢谢你接受调查!你喜欢的{favorite_digital[man]}我也喜欢!')
else:
print(f'{man.title()},快点接受调查吧!')
#练习 6-7:
#用列表装字典
messages_man_1 = {
'first name':'宝宝',
'last name':'海绵',
'age':'35',
'city':'比奇堡海滩'
}
messages_man_2 = {
'first name':'大星',
'last name':'派',
'age':'36',
'city':'比奇堡海滩'
}
messages_man_3 = {
'first name':'哥',
'last name':'章鱼',
'age':'37',
'city':'比奇堡海滩'
}
people = [messages_man_1,messages_man_2,messages_man_3]
i = 0
for man in people:
Full_name = f"{man['last name']}{man['first name']}"
i +=1
print(f'第{i}位登场的是{Full_name}!')
Age = man['age']
loc = man['city']
print(f'\t它今年{Age}岁了\n\t它来自{loc}')
#练习 6-8:
#用列表装字典
pet_1 = {
'pet':'小蜗',
'master':'海绵宝宝'
}
pet_2 = {
'pet':'咪咪',
'master':'蟹老板'
}
pet_3 = {
'pet':'旺财',
'master':'派大星'
}
pets = [pet_1,pet_2,pet_3]
i = 0
for pet in pets:
i +=1
Pet = pet['pet']
Master = pet['master']
print(f'第{i}位主人是{Master},\n\t他的宠物叫{Pet}!')
#练习 6-9:
#用字典装列表
favorite_places = {
'lj':['西藏','华山'],
'cqa':['西藏','泰山'],
'xjs':['西藏','泰山']
}
for name in favorite_places.keys():
print(f'Name:{name.title()}')
print('Favorite_Place:')
for place in favorite_places[name]:
print('\t' + place)
#练习 6-10:
favorite_digital = {'yzj':['7','3'],
'lj':['7','3'],
'ltt':['8',''],
'cqa':['7',''],
'nwh':['0',''],
'xjs':['7','1','9']
}
for name in favorite_digital:
print(f'名字:\t{name.title()}')
print('最喜欢的数字:',end='')
for digital in favorite_digital[name]:
print(digital,end = ' ')
print('\n')
#练习 6-11:
#字典里装字典
cities = {
'new york':{
'country':'america',
'population':'3.3(亿)',
'fact':'Never sleeps city & Statue of Liberty'
},
'sydney':{
'country':'australia',
'population':'0.25(亿)',
'fact':'Sydney & Sydney Opera House'
},
'chang sha':{
'country':'china',
'population':'13.4(亿)',
'fact':'Star City & Orange Isle'
}
}
for city in cities:
print(f'City_name:{city.title()}')
for key,value in cities[city].items():
print(f'\t{key}:{value.title()}')
#练习 6-12:
#总结一下,这章有列表里装字典,字典里装列表,字典里面装字典 以及遍历输出键,值,键与值的方法
第6章--字典
最新推荐文章于 2025-12-05 17:02:52 发布
315

被折叠的 条评论
为什么被折叠?



