6-2 喜欢的数字
代码:
favorite_num = {"Amy": 1, "John": 2, "Lily": 2, "Mike": 5, "Tom": 3} for name, num in favorite_num.items(): print(name + " is fond of " + str(num))
结果:
Amy is fond of 1
John is fond of 2
Lily is fond of 2
Mike is fond of 5
Tom is fond of 3
6-5 河流
代码:
rivers = {"Nile": "Egypt", "Abc": "Def", "Mccp": "Ame"} for river, country in rivers.items(): print(river + " runs through " + country) for river in rivers.keys(): print(river) for country in rivers.values(): print(country)
结果:
Nile runs through Egypt
Abc runs through Def
Mccp runs through Ame
Nile
Abc
Mccp
Egypt
Def
Ame
6-10 喜欢的数字们
代码:
favorite_nums = {"Amy": [1, 2, 5], "John": [2, 7, 9], "Lily": [2, 4], "Mike": [5, 8, 9], "Tom": [3]} for name, nums in favorite_nums.items(): print(name + " is fond of " + str(nums))
结果:
Amy is fond of [1, 2, 5]
John is fond of [2, 7, 9]
Lily is fond of [2, 4]
Mike is fond of [5, 8, 9]
Tom is fond of [3]