Python编程:从入门到实践——字典(第六章+课后答案)

对应书目:Python编程:从入门到实践

字典(第六章)(所有习题答案请自己输入,不要粘贴复制)

更新备忘习题记录

练习6-1:人 使用一个字典来存储一个熟人的信息,包括名,姓,年龄和居住的城市。该字典包含first_name,last_name,age,city.将存储在该字典中的每项信息都打印出来。

people = {
    'first_name':'ming',
    'last_name':'li',
    'age': 23,
    'city': 'jinan'
    }
print(people['first_name'])
print(people['last_name'])
print(people['age'])
print(people['city'])

练习6-2:喜欢的数 使用一个字典来存储一些人喜欢的数。请想出5个人的名字,并将这些名字用作字典中的键;找出每个人喜欢的一个数,并将这些数作为值存储在字典中。打印每个人的名字和喜欢的数。为了让这个程序更有趣,通过询问朋友确保数据的真实。

favorite_numbers = {
    'jen':'5',
    'sarah':'6',
    'edward':'3',
    'phil':'1',
    'molly':'2'}
for name, number in favorite_numbers.items():
    print(f"{name.title()}'s favorite number is {number.title()}.")

练习6-3:词汇表 Python字典可用于模拟现实生活中的词典。为了避免混淆,我们将后者称为词汇表。

  • 想出你在前面学过的5个编程术语,将其用作词汇表中的键,将它们的含义作为值存储在词汇表中。
  • 以整洁的方式打印每个术语及其含义。为此,可先打印术语,在它后面加上一个冒号,再打印含义;也可在一行打印术语,再使用换行符(\n)插入一个空行,然后在下一行以缩进的方式打印其含义。

pythons = {'sort':'permanently modify the order of the list',
           'sorted':'arrang the list in a specific order'
           }
for key,value in pythons.items():
    print(f"\nkey:{key}")
    print(f"value:{value}")

练习6-4:词汇表2 现在你知道了如何遍历字典,可以整理为完成练习6-3而编写的代码,将其中的一系列函数用print()替代为一个遍历字典中值和键的循环。确定该循环正确无误后,再在词汇表中添加5个 python 术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。

练习6-5:河流 创建一个字典,在其中存储三条重要的河流及其流经的国家,例如,一个键值对可能是‘nlie':'egypt.

  • 使用循环为每一条河流打印一条消息,下面是一个例子。The nile runs throuhg Egypt.

rivers = {
    'the changjiang river':'China',
    'nile': 'egypt',
    'river thames':'England'}
for key,value in rivers.items():
    print(f"The {key.title()} runs through {value.title()}.")

  • 使用循环将该字典每一条河流的名字打印出来。

rivers = {
    'the changjiang river':'China',
    'nile': 'egypt',
    'river thames':'England'}
for river in rivers.keys():
    print(river.title())

  • 使用循环将该字典包含的每个国家的名字打印出来。

rivers = {
    'the changjiang river':'China',
    'nile':'egypt',
    'river thames':'england'}
for country in rivers.values():
    print(country.title())

练习6-6:调查 在6.3.1节中编写程序 favorite_languages.py 中执行一下操作:

创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。

遍历这个人员名单。对于已参与调查的人,打印一条消息表示感谢;对于还未参与调查的人,打印一条消息邀请他参加。

favorite_languages = {
    'jen':'python',
    'sarah':'C',
    'edward':'ruby',
    'phil':'python',
    }
requested_names = ['jen','sarah','edward','phil','molly','susan']
for name in favorite_languages.keys():
    print(f"\t{name.title()},thank you for taking the poll")
for requested_name in requested_names:
    if requested_name not in favorite_languages.keys():
        print(f"\t{requested_name}, please take our poll.")

练习6-7 人们 在为完成6-1 编写的程序中,再创建两个表示人的字典,将这三个字典都存储在一个名为people 的列表中。遍历该列表,将其中每个人的所有信息都打印出来。

people_0 = {
    'first_name':'ming',
    'last_name':'li',
    'age': 23,
    'city': 'jinan'
    }
people_1 = {
    'first_name':'hong',
    'last_name':'zhang',
    'age': 15,
    'city': 'qingdao'
    }
people_2 = {
    'first_name':'mei',
    'last_name':'wang',
    'age': 32,
    'city': 'nanjing'
    }
peoples = [people_0, people_1, people_2]
for people in peoples:
    print(people)

练习6-8:宠物 创建多个表示宠物的字典,将每个字典都包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets 的列表中,并将有关每个宠物的所有信息都打印出来。

pet_0 = {
    'names of pets' :'british shorthair cat',
    'types of pets':'cats',
    'owner':'ming li'
    }
pet_1 = {
    'names of pets' :'parrot',
    'types of pets':'birds',
    'owner':'hong zhang'
    }
pet_2 = {
    'names of pets' :'goldfish',
    'types of pets':'fish',
    'owner':'mei wang'
    }
pets = [pet_0, pet_1, pet_2]
for pet in pets:
    print(pet)

练习6-9:喜欢的地方 创建一个名为favorite_places的字典。在这个字典中,将三个人的名字作为键,并存储每个人喜欢的1-3个地方。为了让这个练习更有趣些,可以让一些朋友说出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。

favorite_places = {'ming li': ['beijing','shanghai','tianjin'],
                   'hong zhang': ['qingdao'],
                   'mei wang': ['xuzhou'],}
for name, places in favorite_places.items():
    print(f"\n{name.title()}'s favorite places are:")
    for place in places:
        print(f"\t{place.title()}")

练习6-10:喜欢的数2 修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数,然后将每个人喜欢的名字及其喜欢的数打印出来。

favorite_numbers = {
    'jen':['5','8'],
    'sarah':['6'],
    'edward':['3'],
    'phil':['1'],
    'molly':['2']}
for name, numbers in favorite_numbers.items():
    print(f"{name.title()}'s favorite number are: ")
    for number in numbers:
        print(f"\t{number.title()}")

练习6-11:城市 创建一个名为cities 的字典,将三个城市名用作键。对于每座城市,都创建一个字典,并在其中包含该城市所属的国家,人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country,population和fact 等键。将每座城市的名字以及有关的信息都打印出来。

cities = {
    'beijing':{
        'country':'china',
        'population':'14 million 277 thousand',
        'fact':'solemn',
        },
    'kaifeng':{
        'country':'china',
        'population':'4 million 694 thousand',
        'fact':'classical',
        },
    'shanghai':{
        'country':'china',
        'population':'24 million 758 thousand and 900',
        'fact':'fashion'
        },
    }
for cityname, city_info in cities.items():
    print(f"\ncityname: {cityname}")
    country = city_info['country']
    population = city_info['population']
    fact = city_info['fact']
    print(f"\tcountry:{country.title()}")
    print(f"\tpopulation:{population.title()}")
    print(f"\tfact:{fact.title()}")

练习6-12:扩展 本章的示例足够复杂,能以很多的方式进行扩展,请对本章的一个示例进行扩展:添加键和值,调整程序要解决的问题或改进输出的格式。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值