第六章 字典
6-1 人
personal_information = {
"first_name": "David",
"last_name": "Blade",
"age": 20,
"city": "NewYork"
}
print (personal_information)
6-2 喜欢的数字
favourite_numbers = {
"Alice": 1,
"Bob": 2,
"Cindy": 3,
"David": 4,
"Frank": 5
}
print (favourite_numbers)
6-3 词汇表
Words = {
"int": "an int number",
"float": "a float number",
"double": "a double number",
"for": "a loop's identifier",
"while": "a loop's identifier"
}
for word in Words.keys():
print (word + ": " + Words[word])
6-4 词汇表2
Words = {
"int": "an int number",
"float": "a float number",
"double": "a double number",
"for": "a loop's identifier",
"while": "a loop's identifier"
}
for word, value in Words.items():
print (word + ": " + value)
6-5 河流
Rivers = {
"nile": "egypt",
"changjiang": "china",
"huanghe": "china"
}
for river, country in Rivers.items():
print ("The " + river + " runs through " + country)
for river in Rivers.keys():
print (river)
for country in Rivers.values():
print (country)
6-6 调查
People = ["Alice", "Bob", "Cindy", "David", "Frank", "Eric"]
favorite_languages = {
"jen": "python",
"sarah": "c",
"edward": "ruby",
"Cindy": "python"
}
for name in favorite_languages.keys():
if name in People:
print (name + ", Thank you for investigation!")
else:
print (name + ", We sincerely invite you to our investigation!")
6-10 喜欢的数字
favourite_numbers = {
"Alice": [1, 2, 3],
"Bob": [2, 3, 4],
"Cindy": [4, 5, 6],
"David": [1, 7, 5],
"Frank": [1]
}
print ("The students and their favourite numbers")
for name, numbers in favourite_numbers.items():
print (name + "'s favourite numbers are ", end="")
for number in numbers:
print (str(number) + " ", end="")
print ("")
6-11 城市
Cities = {
"Shenzhen": {"country": "China", "continent": "Asia"},
"Guangzhou": {"country": "China", "continent": "Asia"},
"Xianggang": {"country": "China", "continent": "Asia"},
"Moscow": {"country": "Russia", "continent": "Asia"}
}
for city, information in Cities.items():
print (city + "'s information: ", end="")
for country, continent in Cities[city].items():
print (country + ": "+ continent + " ", end="")
print ("")