《Python编程从入门到实践》Chapter 6练习题

# 6-1 人
information = {'first_name':'Julian','last_name':'Aaron','city':'New York'}

print(information['first_name'])
print(information['last_name'])
print(information['city'])

在这里插入图片描述

#6-3 编程词汇字典
programming_vocabularies = {
    'rstrip':'deleting trailing spaces',
    'lstrip':'deleting the opening space',
    'append':'adding elements at the end of the list',
    'sort':'permanently sorting of lists',
    'pop':'deleting the element and then use its value'
    }
print('\n'+'The meaning of rstrip is '+programming_vocabularies['rstrip']+'.')
print('lstrip'+':'+programming_vocabularies['lstrip'])
print('The meaning of append is '+programming_vocabularies['append']+'.')
print('sort'+':'+programming_vocabularies['sort'])
print('The meaning of pop is '+programming_vocabularies['pop']+'.')

在这里插入图片描述

#6.4 词汇表2(使用for遍历)
print('\n')
programming_vocabularies = {
    'rstrip':'deleting trailing spaces',
    'lstrip':'deleting the opening space',
    'append':'adding elements at the end of the list',
    'sort':'permanently sorting of lists',
    'pop':'deleting the element and then use its value'
    }

for vocabulary,meaning in programming_vocabularies.items():
    print('The meaning of '+ vocabulary +' is '+ meaning+'.')

在这里插入图片描述

#6.5 河流
rivers={
    'nile':'egypt',
    'yangtze river':'china',
    'yellow river':'china'
    }

print('\n')
for river,country in rivers.items():
    print('The '+river.title()+' runs through '+ country.title()+'.')

print('\n')
for river in rivers:
    print(river.title())

print('\n')
for country in rivers.values():
    print(country.title())

print('\n')
for country in set(rivers.values()):
    print(country.title())

在这里插入图片描述

#6-6 调查
favorite_language = {
    'jen':'python',
    'sarah':'c',
    'edward':'ruby',
    'phil':'python',
    }
personnel_list = ['jen','edward','abigail']

print('\n')
for name in personnel_list:
    if name in favorite_language.keys():
        print(name.title()+',thanks for participating in the survey.')
    else:
        print(name.title()+ ',please take our poll!')

在这里插入图片描述

# 6-7人 (列表中嵌套字典)
information_1= {'first_name':'Julian','last_name':'Aaron','city':'New York'}
information_2= {'first_name':'Nanthan','last_name':'Hale','city':' ChengTu'}
information_3= {'first_name':'Oliver','last_name':'Clinton','city':'Sian'}

information = [information_1,information_2,information_3]

for A in information:
    #为了能一个information输一次,理解思想
    print('\n')
    for a,b in A.items():
        print('\t'+a.title()+': '+ b)

在这里插入图片描述

#6-8 宠物(列表中嵌套字典)(作废,再重新读题,想复杂了)
honey={'variety':'persian','master':'Julian'}
candy={'variety':'siamese','master':'Nanthan'}
mark={'variety':'chinchilla','master':'Oliver'}

cats = [honey,candy,mark]

for cat_number in range(3):
    cat1=cats[cat_number] #这是错误的写法。在字典列表(列表中嵌套字典)的情况中,不可能将字典的名称用for循环出来。
    print(cat1.title()+':')
    print('\n')
for cat in cats:
    for key,value in cat.items():
        print('\t'+ key.title()+': '+ value.title())

#在下面这种情况下可以将列表中的元素循环出来,见下例。观察两者的区别。
cats = ['honey','candy','mark']

for cat_number in range(0,3):
    print(cats[cat_number])

#但其实有更简单的方法,所以思想很重要
cats = ['honey','candy','mark']
for cat in cats:
    print(cat)
#6-8 宠物(列表中嵌套字典)
cat = {'type':'persian','variety':'cat','master':'Julian'}
dog ={'type':'siamese','variety':'dog','master':'Nanthan'}
bird={'type':'chinchilla','variety':'bird','master':'Oliver'}

pets = [cat,dog,bird]

for pet in pets:
    print('\n')
    #一定要分清遍历字典时,最多只有两个变量,而且分清哪些是键,哪些是值
    for key,value in pet.items():
        print('\t'+ key.title() + ': '+value.title())

在这里插入图片描述

#6-9 喜欢的地方(字典中嵌套列表)
favorite_places = {
    'Julian':['a','b','c'],
    'Nanthan':['d','e','f'],
    'Oliver':['g','h','i']
    }

for name,palces in favorite_places.items():
    print('\n'+ name.title() + "'s favorite places are ")
    for place in palces:
        print('\t'+ place.title())

在这里插入图片描述
总结:
①6-7,8都是列表中嵌套字典,规律:先遍历列表再遍历字典,变量先一后二;
②6-9,10都是字典中嵌套列表,规律:先遍历字典再遍历列表,变量先二后一。

#6-11 城市(字典中储存列表)
cities ={
    'new york':{
        'country':'America',
        'population':'8.51 million',
        'fact':'beautiful'
        },
    'paris':{
        'country':'France',
        'population':'214 million',
        'fact':'romantic'
        },
    'beijing':{
        'country':'China',
        'population':'217 million',
        'fact':'bustling'
        },
    }

for city,infor in cities.items():
    print('\nCity Name: '+ city.title())
    for key,value in infor.items():
        print('\t'+ key.title() + ': '+ value)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值