person = {
'first_name': 'san',
'last_name': 'zhang',
'age': 20,
'city': 'guangzhou',
}
for key in person:
print(key+': '+str(person[key]))
6-2 喜欢的数字
favourite_nums = {
'alice': 3,
'bob': 1,
'charlie': 4,
'david': 1,
'eva': 5,
}
for key, value in favourite_nums.items():
print(key.title(), 'likes', value)
6-3 词汇表
words = {
'C': 'A programming language.',
'Python': 'A programming language.',
'Java': 'A programming language.',
'Google': 'An IT company.',
'CPU': 'Central Processing Unit.',
}
for key, value in words.items():
print(key+':', value)
6-4 词汇表2
words = {
'C': 'A programming language.',
'Python': 'A programming language.',
'Java': 'A programming language.',
'Google': 'An IT company.',
'CPU': 'Central Processing Unit.',
'Linux': 'An operating system.',
'Windows': 'An operating system.',
'DOS': 'An operating system.',
'UNIX': 'An operating system.',
'Turing': 'A computer scientist.',
}
for key, value in words.items():
print(key+':', value)
6-5 河流
rivers = {
'nile': 'egypt',
'changjiang river': 'china',
'yellow river': 'china',
}
for key, value in rivers.items():
print('The '+key.title()+' runs through '+value.title()+'.')
6-6 调查
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
people = ['jen', 'charlie', 'sarah', 'bob']
for person in people:
if person in favorite_languages:
print(person.title()+',感谢您参与本次调查!')
else:
print(person.title()+',请参与调查!')
6-7 人
person1 = {
'first_name': 'san',
'last_name': 'zhang',
'age': 20,
'city': 'guangzhou',
}
person2 = {
'first_name': 'si',
'last_name': 'li',
'age': 19,
'city': 'shanghai',
}
person3 = {
'first_name': 'wu',
'last_name': 'wang',
'age': 21,
'city': 'shenzhen',
}
people = [person1, person2, person3]
for person in people:
for key in person:
print(key+': '+str(person[key]))
print()
6-8 宠物
Tom = {
'type': 'cat',
'master': 'obasan',
}
Jerry = {
'type': 'mouse',
'master': 'nobody',
}
pets = [Tom, Jerry]
for pet in pets:
for key, value in pet.items():
print(key+': '+value)
print()
6-9 喜欢的地方
favorite_places = {
'alice': ['tokyo', 'dubai', 'newyork'],
'bob': ['shanghai', 'chongqing'],
'charlie': ['london', 'moskow', 'kyoto'],
}
for name, places in favorite_places.items():
print(name.title()+"'s favorite places are:", end=' ')
count = len(places)
for place in places:
if count != 1:
print(place.title(), end=', ')
else:
print(place.title())
count -= 1
6-10 喜欢的数字
favourite_nums = {
'alice': [2,45,15],
'bob': [1,50,2],
'charlie': [4,7,9],
'david': [1,2,6,9],
'eva': [5,15,26,3,2,1],
}
for key, values in favourite_nums.items():
print(key.title(), 'likes', end=' ')
for value in values:
print(value, end=' ')
print()
6-11 城市
cities = {
'tokyo': {
'country': 'Japan',
'population': 13510000,
'fact': 'capital',
},
'beijing': {
'country': 'China',
'population': 21705000,
'fact': 'capital',
},
'shanghai': {
'country': 'China',
'population': 24152700,
'fact': 'international city',
},
}
for city in cities:
print(city.title()+':')
for key, value in cities[city].items():
print('\t'+key+':', value)
6-12 扩展
cities = {
'tokyo': {
'country': 'japan',
'population': 13510000,
'fact': 'capital',
},
'beijing': {
'country': 'china',
'population': 21705000,
'fact': 'capital',
},
'shanghai': {
'country': 'china',
'population': 24152700,
'fact': 'international city',
},
}
for city in cities:
print(city.title()+':')
for key, value in cities[city].items():
print('\t'+key+':', end=' ')
if key == 'country':
print(value.title())
else:
print(value)