6-1 人
message_person = {
'first_name': 'Hua',
'last_name': 'Li',
'age': 20,
'city': 'Guangzhou'
}
for key, value in message_person.items():
print(key, " : ", value)
输出:
first_name : Hua
last_name : Li
age : 20
city : Guangzhou
6-5 河流
rivers = {
'nile': 'egypt',
'Yangtze River': 'China',
'Amazonian River': 'Brazil',
'Thames': 'England'
}
for river, country in rivers.items():
print("The " + river.title() + " runs through " + country.title() + ".")
for river in rivers.keys():
print(river.title())
for country in rivers.values():
print(country.title())
输出:
The Nile runs through Egypt.
The Yangtze River runs through China.
The Amazonian River runs through Brazil.
The Thames runs through England.
Nile
Yangtze River
Amazonian River
Thames
Egypt
China
Brazil
England
6-7 人
message_person1 = {
'first_name': 'Hua',
'last_name': 'Li',
'age': 20,
'city': 'Guangzhou'
}
message_person2 = {
'first_name': 'Lin',
'last_name': 'Wang',
'age': 22,
'city': 'Beijing'
}
message_person3 = {
'first_name': 'Wen',
'last_name': 'Cai',
'age': 30,
'city': 'Shanghai'
}
people = [message_person1, message_person2, message_person3]
for person in people:
for key, value in person.items():
print(key, " : ", value)
输出:
first_name : Hua
last_name : Li
age : 20
city : Guangzhou
first_name : Lin
last_name : Wang
age : 22
city : Beijing
first_name : Wen
last_name : Cai
age : 30
city : Shanghai
6-10 城市
cities = {
'Guangzhou': {
'country': 'China',
'population': '14 million',
'fact': 'Guangzhou is the capital of Guangdong, also known as Huacheng and Yangcheng'
},
'New York': {
'country': 'U.S.A',
'population': '7 million',
'fact': 'New York is a multi ethnically diverse city.'
},
'London': {
'country': 'Britain',
'population': '60 million',
'fact': 'London has a large number of scenic spots and museums.'
}
}
for name, city in cities.items():
print(name + " belongs to " + city['country'] + ", with a population of " + city['population'] + ".")
print(city['fact'] + "\n")
输出:
Guangzhou belongs to China, with a population of 14 million.
Guangzhou is the capital of Guangdong, also known as Huacheng and Yangcheng
New York belongs to U.S.A, with a population of 7 million.
New York is a multi ethnically diverse city.
London belongs to Britain, with a population of 60 million.
London has a large number of scenic spots and museums.