Python习题五--Python编程:从入门到实践 6.5~6.11


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

people={'first_name':'jay','last_name':'zhou','age':'18','city':'beijing'}
print(people)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
{'first_name': 'jay', 'last_name': 'zhou', 'age': '18', 'city': 'beijing'}

Process finished with exit code 0

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

people={'zz':1,'xx':2,'cc':3,'vv':4,'bb':5}
print(people)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
{'zz': 1, 'xx': 2, 'cc': 3, 'vv': 4, 'bb': 5}

Process finished with exit code 0

6-3 词汇表 :Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。 想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n )插 入一个空行,然后在下一行以缩进的方式打印词汇的含义。

vocabulary={'print':'打印','int':'整型','char':'字符','scanf':'输出','float':'浮点'}
for i,j in vocabulary.items():
    print(i,':',j)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
print : 打印
int : 整型
char : 字符
scanf : 输出
float : 浮点

Process finished with exit code 0

知识点 items()函数:

items( )用于 返回一个字典的拷贝列表【Returns a copy of the list of all items (key/value pairs) in D】,占额外的内存。

>>> help(dict.items)
Help on method_descriptor:

items(...)
    D.items() -> a set-like object providing a view on D's items

可以用来循环遍历。

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

6-5创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是’nile’: ‘egypt’ 。
使用循环为每条河流打印一条消息,如“The Nileruns throughEgypt.”。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。

vocabulary={'nile':'egypt','huanghe':'china','changjiang':'china'}
for i,j in vocabulary.items():
    print('the '+i+'runs through '+j)
for i in vocabulary.keys():
    print(i.title())
for j in vocabulary.values():
    print(j.title())
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
the nileruns through egypt
the huangheruns through china
the changjiangruns through china
Nile
Huanghe
Changjiang
Egypt
China
China

Process finished with exit code 0

6-6 调查 :在6.3.1节编写的程序favorite_languages.py中执行以下操作。 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。

favorite_languages = { 'jen': 'python',
                       'sarah': 'c',
                       'edward': 'ruby',
                       'phil': 'python', }
survey={'jen': 'python',
        'sarah': 'c',
        'edward': 'ruby',
        'phil': 'python',
        'jhu':'c++',
        'uwey':'hhh'}
for name in survey:
  if name in favorite_languages:
    print(name.title()+', Thank you for take part in our survey')
  else:
    print(name.title()+", please take part in our survey")
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py
Jen, Thank you for take part in our survey
Sarah, Thank you for take part in our survey
Edward, Thank you for take part in our survey
Phil, Thank you for take part in our survey
Jhu, please take part in our survey
Uwey, please take part in our survey

Process finished with exit code 0

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

users={'people1':{'first_name':'jay','last_name':'zhou','age':'18','city':'beijing'},
        'people2':{'first_name':'oiu','last_name':'plu','age':'12','city':'guangzhou'},
        'people3': {'first_name': 'sdl', 'last_name': 'vfw', 'age': '34', 'city': 'changsha'} }

for username,user_info in users.items():
    print("\nUsername:"+username)
    full_name=user_info['first_name']+" "+user_info['last_name']
    location=user_info['city']
    print("\tFull name:"+full_name.title())
    print("\tLocation:"+location.title())
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

Username:people1
	Full name:Jay Zhou
	Location:Beijing

Username:people2
	Full name:Oiu Plu
	Location:Guangzhou

Username:people3
	Full name:Sdl Vfw
	Location:Changsha

Process finished with exit code 0

6-8 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets 的列表中,再遍历该列表,并将宠物的所有信息都打印出来。

pets={'rabbit':{'type':'啮齿','pet owner':'july'},
      'cat':{'type':'猫科','pet owner':'joy'},
      'dog':{'type':'犬类','pet owner':'anny'}}
for pet,pet_info in pets.items():
    print('\nPet name:'+' '+pet)
    print('\tPet type:'+' '+pet_info['type'])
    print('\tPet owner:'+' '+pet_info['pet owner'])
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

Pet name: rabbit
	Pet type: 啮齿
	Pet owner: july

Pet name: cat
	Pet type: 猫科
	Pet owner: joy

Pet name: dog
	Pet type: 犬类
	Pet owner: anny

Process finished with exit code 0

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

favourite_places={'jj':['a','b'],
                  'dd':['c','d'],
                  'aa':['e','f']}
for name,places  in favourite_places.items():
    print('\n'+name.title()+"'s favourite places are:")
    for place in places:
        print('\t'+place.title())
favourite_places={'jj':['a','b'],
                  'dd':['c','d'],
                  'aa':['e','f']}
for name,places  in favourite_places.items():
    print('\n'+name.title()+"'s favourite places are:")
    for place in places:
        print('\t'+place.title())

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

people={'zz':['1','2'],'xx':['3','4'],'cc':['5','7'],'vv':['1','7'],'bb':['4','8']}
for name,numbers in people.items():
    print('\n'+name.title()+"'s favourite numbers are")
    for number in numbers:
        print('\t'+number)
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

Zz's favourite numbers are
	1
	2

Xx's favourite numbers are
	3
	4

Cc's favourite numbers are
	5
	7

Vv's favourite numbers are
	1
	7

Bb's favourite numbers are
	4
	8

Process finished with exit code 0

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

cities={'London':{'country':'England','population':'111','fact':'qqq'},
        'Beijing':{'country':'China','population':'222','fact':'www'},
        'New York':{'country':'USA','population':'333','fact':'eee'}}
for city,city_info in cities.items():
    print('\nName:'+city)
    print('\tCountry:'+city_info['country'])
    print('\tPopulation:'+city_info['population'])
    print('\tFact:'+city_info['fact'])
D:\pythonProject\venv\Scripts\python.exe D:/pythonProject/first.py

Name:London
	Country:England
	Population:111
	Fact:qqq

Name:Beijing
	Country:China
	Population:222
	Fact:www

Name:New York
	Country:USA
	Population:333
	Fact:eee

Process finished with exit code 0

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

看星河的兔子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值