#6-5
rivers = {
'nile' : 'egypt',
'yellow' : 'china',
'zhu' : 'china'
}
for river, country in rivers.items():
print(river.title() + " is in " + country.title())
for river in rivers.keys():
print(river.title())
for country in rivers.values():
print(country)
print("\n")
#6-6
asked = {
'bob' : 'python',
'tan' : 'c++'
}
should_ask = ['tan', 'bob', 'wang', 'jack']
for people in should_ask:
if people in asked.keys():
print(people.title() + ", thank you very much!")
else:
print(people.title() + ", would you like to join us?")
print("\n")
#6-8
pet_list = [
{'animal':'dog', 'owner':'jack'},
{'animal':'cat', 'owner':'phd'}
]
for pet in pet_list:
print("This pet's name is " + pet['animal'].title())
print("\tIts owner is " + pet['owner'].title())
print("\n")
#6-9
favorite_places = {
'bill' : ['china', 'italy'] ,
'wang' : ['america', 'north korea', 'japan'],
'jack' : ['computer room']
}
for people, places in favorite_places.items():
print(people.title() + " likes:")
for place in places:
print("\t" + place.title())
print("\n")
#6-11
cities = {
'Beijing' : {
'country':'china',
'population':'a lot',
'fact':'capital'},
'London' : {
'country':'england',
'population':'a lot',
'fact':'capital'},
'Guiyang' : {
'country':'china',
'population':'a little',
'fact':'nice'},
}
for city, fact in cities.items():
print(city.title()+" infromation:")
for member, information in fact.items():
print("\t" + member.title() + " : " + information.title())
print("\n")
运行结果如下:
Nile is in Egypt
Yellow is in China
Zhu is in China
Nile
Yellow
Zhu
egypt
china
china
Tan, thank you very much!
Bob, thank you very much!
Wang, would you like to join us?
Jack, would you like to join us?
This pet's name is Dog
Its owner is Jack
This pet's name is Cat
Its owner is Phd
Bill likes:
China
Italy
Wang likes:
America
North Korea
Japan
Jack likes:
Computer Room
Beijing infromation:
Country : China
Population : A Lot
Fact : Capital
London infromation:
Country : England
Population : A Lot
Fact : Capital
Guiyang infromation:
Country : China
Population : A Little
Fact : Nice