6.2习题代码
# 6-1 人
information = {
'first_name': 'li',
'last_name': 'jake',
'age': '18',
'city': 'guangzhou'
}
print(information['first_name'])
print(information['last_name'])
print(information['age'])
print(information['city'])
print()
# 6-2 喜欢的数字
like_nums = {
"alex": 3,
"jake": 2,
"yi": 6,
"wang": 5,
"he": 8,
}
for name,value in like_nums.items():
print(name + " like the numbers is " + str(value))
print()
# 6-3 词汇表
words = {
"len": "total the list length",
"title": "can express the world",
"max": "get the number max",
"min": "get the number min",
"sort": "arrange the order"
}
for word, explain in words.items():
print(word + " : " + explain + "\n")
6.3习题代码
# 6-5 河流
rivers = {
"niuniu": "China",
"lulu": "Japan",
"nile": "Europe",
}
for river, country in rivers.items():
print("The "+river.upper()+" run throngh " + country.upper())
print()
for river in rivers.keys():
print(river)
print()
for country in rivers.values():
print(country)
print()
# 6-6 调查
list = ["jake", "herry", "bob", "niuma", "zip"]
name_information = {
"jake": "c",
"bob": "python",
"niuma": "java"
}
for name in list:
if name in name_information.keys():
print(name + " Thank you can join the survey\n")
else:
print(name + " I think you can join the survey\n")
6.4习题代码
#6-7人
person1 = {
"name": "jake",
"age": 18,
"sex": "man"
}
person2 = {
"name": "judy",
"age": 18,
"sex": "man"
}
person3 = {
"name": "bob",
"age": 18,
"sex": "man"
}
people = [person1, person2, person3]
for i in people:
print(i)
#6-8 宠物
pet1 = {'owner': "bob", 'type': "dog"}
pet2 = {'owner': "alex", 'type': "cat"}
pet3 = {'owner': "jake", 'type': "pig"}
pets = [pet1, pet2, pet3]
for i in pets:
print(i)
#6-9 最喜欢的地方
favorite_palce ={
"jake": ["hunan", "shanghai"],
"bob": ["jiangxi", "guanzhou"],
}
for name,places in favorite_palce.items():
print(name + " the favorite place is:" )
for place in places:
print(place)
#6-10 喜欢的数字
like_nums = {
"alex": [3, 33, 333],
"jake": [6, 66, 666],
"yi": [8,88,888],
"wang": [9,99,999],
"he": [1, 11, 111],
}
for name,value in like_nums.items():
print(name + " like the numbers is " + str(value))
print()
#6-11 城市
cities = {
"shanghai": {"country": "china", "population": "1000千万", "fact":"this is a good city"},
"beijing": {"country": "china", "population": "1001千万", "fact":"this is a good city"},
"shanxi": {"country": "china", "population": "1002千万", "fact":"this is a good city"},
}
for name, values in cities.items():
print(name + " " + values["country"] + " " +values["population"] + " " + values["fact"])