6-1 人 : 使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键first_name 、last_name 、age 和city 。将存储在该字典中的每项信息都打印出来。
代码:
people = {"first_name": "Wilson", "last_name": "John", "age": 20, "city": "America"}
print(people["first_name"])
print(people["last_name"])
print(people["age"])
print(people["city"])
结果:
Wilson
John
20
America
6-2 喜欢的数字 :使用一个字典来存储一些人喜欢的数字。请想出5个人的名字,并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存储在字典中。打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的。
代码:
favor_num = {
"Jack": 10,
"Amy": 2,
"Tim": 8,
"Tom": 6,
"Alice": 9
}
print("Jack's favorite number is " + str(favor_num["Jack"]))
print("Amy's favorite number is " + str(favor_num["Amy"]))
print("Tim's favorite number is " + str(favor_num["Tim"]))
print("Tom's favorite number is " + str(favor_num["Tom"]))
print("Alice's favorite number is " + str(favor_num["Alice"]))
结果:
Jack's favorite number is 10
Amy's favorite number is 2
Tim's favorite number is 8
Tom's favorite number is 6
Alice's favorite number is 9
6-5 河流 :创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键—值对可能是'nile': 'egypt' 。
使用循环为每条河流打印一条消息,如“The Nile runs through Egypt.”。
使用循环将该字典中每条河流的名字都打印出来。
使用循环将该字典包含的每个国家的名字都打印出来。
代码:
rivers = {
"nile": "egypt",
"rhein": "switzerland",
"seine": "france",
}
for river, country in rivers.items():
print("The " + river.title() + " runs through " + country.title() + " .")
for name in rivers.keys():
print(name.title())
结果:
The Nile runs through Egypt .
The Rhein runs through Switzerland .
The Seine runs through France .
Nile
Rhein
Seine
6-6 调查 :在6.3.1节编写的程序favorite_languages.py中执行以下操作。
创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。
遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
代码:favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python'
}
people = ["jen", "edward", "jack","phil","alice", "sarah"]
for name in people:
print(name.title())
if name in favorite_languages.keys():
print(" Hi " + name.title()+", thank you for taking part in my investigation!")
else :
print(" Hi " + name.title()+", I would like to invite you to take part in my investigation!")
结果:
Jen
Hi Jen, thank you for taking part in my investigation!
Edward
Hi Edward, thank you for taking part in my investigation!
Jack
Hi Jack, I would like to invite you to take part in my investigation!
Phil
Hi Phil, thank you for taking part in my investigation!
Alice
Hi Alice, I would like to invite you to take part in my investigation!
Sarah
Hi Sarah, thank you for taking part in my investigation!
6-7 人 :在为完成练习6-1而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为people 的列表中。遍历这个列表,将其中每个人的所有信息都打印出来。
代码:
peo_a = {"first_name": "Wilson", "last_name": "John", "age": 20, "city": "America"}
peo_b = {"first_name": "Chen", "last_name": "Jie", "age": 21, "city": "China"}
peo_c = {"first_name": "Smith", "last_name": "George", "age": 22, "city": "America"}
people = [peo_a, peo_b, peo_c]
for peo in people:
print(peo)
结果:
{'first_name': 'Wilson', 'last_name': 'John', 'age': 20, 'city': 'America'}
{'first_name': 'Chen', 'last_name': 'Jie', 'age': 21, 'city': 'China'}
{'first_name': 'Smith', 'last_name': 'George', 'age': 22, 'city': 'America'}
6-8 宠物 :创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为pets的列表中,再遍历该列表,并将宠物的所有信息都打印出来。
代码:
pet_a = {"kind": "cat", "owner": "John"}
pet_b = {"kind": "dog", "owner": "Jack"}
pet_c = {"kind": "bird", "owner": "George"}
pets = [pet_a, pet_b, pet_c]
for pet in pets:
print(pet)
结果:
{'kind': 'cat', 'owner': 'John'}
{'kind': 'dog', 'owner': 'Jack'}
{'kind': 'bird', 'owner': 'George'}
6-9 喜欢的地方 :创建一个名为favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的1~3个地方。为让这个练习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
代码:
favorite_places = {
"Jack": ["Beijing", "America", "Paris"],
"Tom": ["Shanghai", "Hangzhou"],
"Alice": ["Provence"],
}
for name,places in favorite_places.items():
if len(places)==1 :
print(name+"'s favorite place is: ")
else :
print(name+"'s favorite place are: ")
for place in places:
print("\t" + place)
结果:
Jack's favorite place are:
Beijing
America
Paris
Tom's favorite place are:
Shanghai
Hangzhou
Alice's favorite place is:
Provence
6-10 喜欢的数字 :修改为完成练习6-2而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来。
代码:
favor_num = {
"Jack": ["10","18"],
"Amy": ["2","88"] ,
"Tim": ["8","13"],
"Tom": ["6","66","9"],
"Alice": ["999"],
}
for name, nums in favor_num.items()a:
if len(nums)==1:
print(name + "'s favorite number is :")
else :
print(name + "'s favorite numbers are: ")
for num in nums:
print("\t" + num)
结果:
Jack's favorite numbers are:
10
18
Amy's favorite numbers are:
2
88
Tim's favorite numbers are:
8
13
Tom's favorite numbers are:
6
66
9
Alice's favorite number is :
999
6-11 城市 :创建一个名为cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该城市的事实。在表示每座城市的字典中,应包含country 、population 和fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
代码:
cities = {
"Beijing": {
"country": "China",
"population": "21 million",
"fact": "It is the capital of People's Republic of China",
},
"New York":{
"country": "America",
"population": "8 million",
"fact": "It is the largest city in the United States",
},
"London" : {
"country": "England",
"population": "8 million" ,
"fact": "It is one of the largest financial centers in the world",
}
}
for city, city_info in cities.items():
print("\nCity's name: " + city)
print("Country: "+ city_info["country"])
print("Population: "+ city_info["population"])
print("Fact: "+ city_info["fact"])
结果:
City's name: Beijing
Country: China
Population: 21 million
Fact: It is the capital of People's Republic of China
City's name: New York
Country: America
Population: 8 million
Fact: It is the largest city in the United States
City's name: London
Country: England
Population: 8 million
Fact: It is one of the largest financial centers in the world