6-3
cihuibiao = {'for' : 'xunhuan','if' : 'judge','while' : 'xunhuan','int' : 'zhengshu','char' : 'letter'}
for key,value in cihuibiao.items():
print(key + " : " + value)
输出结果:
int : zhengshu
while : xunhuan
char : letter
for : xunhuan
if : judge
6-5
rivers = {'Nile' : 'Egypt','Changjiang' : 'China','Amazon' : 'Brazil'}
for key,value in rivers.items():
print("The " + key + " runs through " + value)
for key in rivers.keys():
print(key)
for value in rivers.values():
print(value)
输出结果:
The Amazon runs through Brazil
The Changjiang runs through China
The Nile runs through Egypt
Amazon
Changjiang
Nile
Brazil
China
Egypt
6-8
Tom = {
'name' : 'Tom',
'type' : 'cat',
'owner' : 'Kangkang'
}
Jake = {
'name' : 'Jake',
'type' : 'mouse',
'owner' : 'Bob'
}
Wzx = {
'name' : 'Wzx',
'type' : 'monkey',
'owner' : 'Yzh'
}
pets = [Tom,Jake,Wzx]
for pet in pets:
for key,value in pet.items():
print(key + " : " + value)
输出结果:
owner : Kangkang
type : cat
name : Tom
owner : Bob
type : mouse
name : Jake
owner : Yzh
type : monkey
name : Wzx
6-9
Nanking = {
'name' : 'Nanking',
'country' : 'China',
'population' : "653w",
'fact' : 'Nanking Massacre'
}
London = {
'name' : 'London',
'country' : 'England',
'population' : '50w',
'fact' : '2012 Olypimc'
}
Sichuan = {
'name' : 'Sichuan',
'country' : 'China',
'population' : '8204w',
'fact' : '2008 earthquake'
}
cities = [Nanking,London,Sichuan]
for city in cities:
for key,value in city.items():
if key == 'name':
print("The city's name is " + value)
elif key == 'country':
print(city['name'] + " locates in " + value)
elif key == 'population':
print("The population of " + city['name'] + " is " + value)
else:
print(value + " happened in " + city['name'])
输出结果:
Nanking Massacre happened in Nanking
Nanking locates in China
The city's name is Nanking
The population of Nanking is 653w
2012 Olypimc happened in London
London locates in England
The city's name is London
The population of London is 50w
2008 earthquake happened in Sichuan
Sichuan locates in China
The city's name is Sichuan
The population of Sichuan is 8204w