第6章--字典

#练习 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:
#总结一下,这章有列表里装字典,字典里装列表,字典里面装字典 以及遍历输出键,值,键与值的方法
        
        
        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-阿呆-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值