【Python编程:从入门到实践】Chapter6字典练习题选做

选做的题目:练习6-9、练习6-11。

练习6-9:喜欢的地方

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

代码1:

favorite_places = {
    'Alice':['Beijing', 'Tianjin', 'Shanghai'],
    'Betty':['Guangzhou'],
     'Cathy':['Suzhou', 'Hangzhou']
     }

for name,place in favorite_places.items():
    print(f'{name}\'s favorite city: ')
    if len(place) == 1:
        print(f'\t{place[0]}')
    else:
        for the_place in place:
            print(f'\t{the_place}')

运行结果:

Alice's favorite city:
        Beijing
        Tianjin
        Shanghai
Betty's favorite city:
        Guangzhou
Cathy's favorite city:
        Suzhou
        Hangzhou

代码2:

使用if语句,将len(place)得到的元素个数作为判断条件。

(注意:这里我的代码会重复输出’somebody‘s favorite city is xxx。)

favorite_places = {
    'Alice': ['Beijing', 'Tianjin', 'Shanghai'],
    'Betty': ['Guangzhou'],
    'Cathy': ['Suzhou', 'Hangzhou']
     }

for name,place in favorite_places.items():
    if len(place) == 1:
        print(f'{name}\'s favorite place is {place[0]}. ')
    else:
        for the_place in place:
            print(f'{name}\'s favorite place is {the_place}. ')

运行结果:

Alice's favorite place is Beijing.
Alice's favorite place is Tianjin.
Alice's favorite place is Shanghai.
Betty's favorite place is Guangzhou.
Cathy's favorite place is Suzhou.
Cathy's favorite place is Hangzhou.

练习6-11:城市

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

代码1:

cities = {
    'Beijing': {
        'country': 'China',
        'population': '1_000_000',  # 随便打的数
        'fact': 'ancient'},
    
    'Tokyo': {
        'country': 'Japan',
        'population': '1_000_000',
        'fact': 'capital'}
        }

for name, information in cities.items():
    print(f'{name}: ')
    print (f"\tcountry: {information['country']}")
    print (f"\tpopulation: {information['population']}")
    print (f"\tfact: {information['fact']}")

# OR

for name, information in cities.items():
    print(f'{name}:')
    country = information['country']
    population = information['population']
    fact = information['fact']
    print(f'\tcountry: {country}')
    print(f'\tpopulation: {population}')
    print(f'\tfact: {fact}')

运行结果:

Beijing:
        country: China
        population: 1_000_000
        fact: ancient
Tokyo:
        country: Japan
        population: 1_000_000
        fact: capital

代码2:

cities = {
    'Beijing': {
        'country': ['China'],
        'population': ['1_000_000'],
        'fact': ['ancient']},
    
    'Tokyo': {
        'country': ['Japan'],
        'population': ['1_000_000'],
        'fact': ['capital']}
        }

for name, information in cities.items():
    print(f'{name}: ')
    for country in information['country']: 
        print (f'\tcountry: {country}')
    for population in information['population']: 
        print (f'\tpopulation: {population}')
    for fact in information['fact']:
        print(f'\tfact: {fact}')

如果“fact”里有很多值,可以使用列表的形式。虽然这个字典里只有一个值,但还是写一下吧。

运行结果:

Beijing:
        country: China
        population: 1_000_000
        fact: ancient
Tokyo:
        country: Japan
        population: 1_000_000
        fact: capital

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值