【Hello Python World】Week 3(2):字典

6-1 人

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

>>> {'first_name':'JP', 'last_name':'Wang', 'age':19, 'city':'Swatow'}
#输出 {'city': 'Swatow', 'first_name': 'JP', 'last_name': 'Wang', 'age': 19}

6-2 喜欢的数字

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

fav_nums = {'J':2, 'P':6, 'JP':26, 'JJ':22, 'PP':66}
for people in fav_nums:
    print(people + ' likes ' + str(fav_nums[people]) + ' most. ')
#输出:
#P likes 6 most.
#PP likes 66 most.
#J likes 2 most.
#JP likes 26 most.
#JJ likes 22 most.

6-3 词汇表

Python字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。

  • 想出你在前面学过的5个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。
  • 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(\n )插 入一个空行,然后在下一行以缩进的方式打印词汇的含义。
language = {'str()':'Change other types into string', 'len()':'Get length of an array', 'del':'Delete a member in an union type', 'for':'Means loop', 'title()':'Print a string with an UPPER first char'}
print('str() : ' + language['str()'] + '.')
print('len() : ' + language['len()'] + '.')
print('del : ' + language['del'] + '.')
print('for : ' + language['for'] + '.')
print('title() : ' + language['title()'] + '.')
#输出:
#str() : Change other types into string
#del : Delete a member in an union type
#title() : Print a string with an UPPER first char
#for : Means loop
#len() : Get length of an array

6-4 词汇表2

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

language = {'str()':'Change other types into string', 'len()':'Get length of an array', 'del':'Delete a member in an union type', 'for':'Means loop', 'title()':'Print a string with an UPPER first char'}
for key, value in language.items():
     print(key + ' : ' + value)
#输出:
#str() : Change other types into string
#del : Delete a member in an union type
#title() : Print a string with an UPPER first char
#for : Means loop
#len() : Get length of an array

6-5 河流

创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt'

  • 使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”
  • 使用循环将该字典中每条河流的名字都打印出来。
  • 使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {'Yangtze':'China', 'Mississippi':'America', 'Nile':'Egypt'}
for key, value in rivers.items():
    print('The ' + key + ' runs through ' + value)
#输出:
#The Yangtze runs through China
#The Mississippi runs through America
#The Nile runs through Egypt
for key in rivers.keys():
    print(key)
#输出:
#Yangtze
#Mississippi
#Nile
for value in rivers.values():
    print(value)
#输出:
#China
#America
#Egypt

6-6 调查

6.3.1节编写的程序favorite_languages.py中执行以下操作。

  • 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。
  • 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
fav_lans = {'JJ':'PHP', 'JP':'Java', 'PP':'C++'}
ask = ['JP', 'PJ', 'JP']
for people in ask:
    if people not in fav_lans.keys():
        print(people + ', welcome to our survey!')
    else:
        print(people + ', thank you for coming.')
#输出:
#JP, thank you for coming.
#PJ, welcome to our survey!
#JP, thank you for coming.

6-7 人

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

fav_nums1 = {'J1':2, 'P1':6, 'JP1':26, 'JJ1':22, 'PP1':66}
fav_nums2 = {'J2':22, 'P2':66, 'JP2':2626, 'JJ2':2222, 'PP2':6666}
fav_nums3 = {'J3':222, 'P3':666, 'JP3':262626, 'JJ3':222222, 'PP3':666666}
fav_nums = [fav_nums1, fav_nums2, fav_nums3]
for fav_num in fav_nums:
    for key, value in fav_num.items():
        print(key + ' : ' + str(value))
    print("")
#输出:
#JP1 : 26
#PP1 : 66
#P1 : 6
#J1 : 2
#JJ1 : 22

#P2 : 66
#PP2 : 6666
#JP2 : 2626
#JJ2 : 2222
#J2 : 22

#PP3 : 666666
#P3 : 666
#JP3 : 262626
#JJ3 : 222222
#J3 : 222

6-8 宠物

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

mimi = {'type':'cat', 'owner':'JJ'}
wnwn = {'type':'dog', 'owner':'PP'}
jiji = {'type':'bird', 'owner':'JP'}
pets = [mimi, wnwn, jiji]
for pet in pets:
    print("I am a " + pet['type'] + " of " + pet['owner'] + ".")
#输出:
#I am a cat of JJ.
#I am a dog of PP.
#I am a bird of JP.

6-9 喜欢的地方

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

favorite_places = {'JP':['Swatow','Chaozhou','Jieyang'], 'JJ':['Beijing', 'Shanghai', 'Guangzhou'], 'PP':['Hong Kong', 'Macao', 'Taiwan']}
for key, value in favorite_places.items():
    print('Hey! I am ' + key + ', I love ')
    for place in value:
        print(place)
    print("")
#输出:
#Hey! I am PP, I love
#Hong Kong
#Macao
#Taiwan

#Hey! I am JP, I love
#Swatow
#Chaozhou
#Jieyang

#Hey! I am JJ, I love
#Beijing
#Shanghai
#Guangzhou

6-10 喜欢的数字

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

fav_nums = {'J':[2,3,4], 'P':[6,7,8], 'JP':[26,27,28], 'JJ':[22,23,24], 'PP':[66,67,68]}
for key, value in fav_nums.items():
    print("Hey! I am " + key + ', I like the number')
    for num in value:
        print(num)
    print("")
#输出:
#Hey! I am P, I like the number
#6
#7
#8

#Hey! I am PP, I like the number
#66
#67
#68

#Hey! I am J, I like the number
#2
#3
#4

#Hey! I am JP, I like the number
#26
#27
#28

#Hey! I am JJ, I like the number
#22
#23
#24

6-11 城市

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

cities = {'Guangzhou':{'country':'China', 'population':10000000, 'fact':'Being called "Yangcheng"'}, 'New York':{'country':'USA', 'population':15000000, 'fact':'Mega City of USA'}, 'Tokyo':{'country':'Japan', 'population':8000000, 'fact':'The location of the Tokyo University'}}
for K, V in cities.items():
    print("Let's talk about the city " + K + ' is a city of ' + V['country'] + ', and it has about ' + str(V['population']) + ', a fact about it is that ' + V['fact'])
#输出:
#Let's talk about the city Guangzhou is a city of China, and it has about 10000000, a fact about it is that Being called "Yangcheng"
#Let's talk about the city New York is a city of USA, and it has about 15000000, a fact about it is that Mega City of USA
#Let's talk about the city Tokyo is a city of Japan, and it has about 8000000, a fact about it is that The location of the Tokyo University

6-12 扩展

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

#示例:求学生的成绩平均值(向下取整)
stu1 = {'name':'jp', 'age':19, 'scores':{'math':98, 'chinese':99, 'english':97}}
stu2 = {'name':'pj', 'age':91, 'scores':{'math':89, 'chinese':99, 'english':79}}
stu3 = {'name':'pp', 'age':99, 'scores':{'math':88, 'chinese':99, 'english':77}}
stus = [stu1, stu2, stu3]
for stu in stus:
    print(stu['name'] + ' : ' + str( sum( stu['scores'].values() ) / len( stu['scores']) ))
#输出:
#jp : 98
#pj : 89
#pp : 88

猜你喜欢

转载自blog.csdn.net/u013159381/article/details/79608954