6-1 人: 使用一个字典来存储一个熟人的信息,包括名、姓、年龄和居住的城市。该字典应包含键 first_name 、 last_name 、 age 和 city 。将存储在该字典中 的每项信息都打印出来。
people = { 'first_name': 'Smith', 'last_name': 'Tom', 'age': '18', 'city': 'Shanghai' } print(people)
{'first_name': 'Smith', 'last_name': 'Tom', 'age': '18', 'city': 'Shanghai'} Process finished with exit code 06-2 喜欢的数字: 使用一个字典来存储一些人喜欢的数字。请想出 5 个人的名字,并将这些名字用作字典中的键;想出每个人喜欢的一个数字,并将这些数字作为值存 、 储在字典中。打印每个人的名字和喜欢的数字。为让这个程序更有趣,通过询问朋友确保数据是真实的。
numbers = {'Tom': 5, 'Hu': 87, 'Sui': 15, 'Jin': 16, 'Bao': 666} print(numbers['Tom']) print(numbers['Jin']) print(numbers['Bao']) print(numbers['Hu']) print(numbers['Sui'])
5 16 666 87 15 Process finished with exit code 06-3 词汇表: Python 字典可用于模拟现实生活中的字典,但为避免混淆,我们将后者称为词汇表。 想出你在前面学过的5 个编程词汇,将它们用作词汇表中的键,并将它们的含义作为值存储在词汇表中。 以整洁的方式打印每个词汇及其含义。为此,你可以先打印词汇,在它后面加上一个冒号,再打印词汇的含义;也可在一行打印词汇,再使用换行符(n )插 入一个空行,然后在下一行以缩进的方式打印词汇的含义。
vocabulary = {'php': '技术语言1', 'python': '技术语言2', 'java': '技术语言3', 'sql': '技术语言4'} print("php: n" + vocabulary['php']) print("python: n" + vocabulary['python']) print("java: n" + vocabulary['java']) print("sql: n" + vocabulary['sql'])
php: 技术语言1 python: 技术语言2 java: 技术语言3 sql: 技术语言4 Process finished with exit code 06-4 词汇表2: 既然你知道了如何遍历字典,现在请整理你为完成练习 6-3 而编写的代码,将其中的一系列 print 语句替换为一个遍历字典中的键和值的循环。确定该 循环正确无误后,再在词汇表中添加 5 个 Python 术语。当你再次运行这个程序时,这些新术语及其含义将自动包含在输出中。
vocabulary = {'php': '技术语言1', 'python': '技术语言2', 'java': '技术语言3', 'sql': '技术语言4'} for key, value in vocabulary.items(): print("nkey: " + key) print("value: " + value) vocabulary['bash']: '技术语言5' vocabulary['C++']: '技术语言6' vocabulary['CSS']: '技术语言7' vocabulary['HTML']: '技术语言8' vocabulary['go']: '技术语言9' for key, value in vocabulary.items(): print("nkey1: " + key) print("value1: " + value)
key: php value: 技术语言1 key: python value: 技术语言2 key: java value: 技术语言3 key: sql value: 技术语言4 key1: php value1: 技术语言1 key1: python value1: 技术语言2 key1: java value1: 技术语言3 key1: sql value1: 技术语言4 Process finished with exit code 06-5 河流: 创建一个字典,在其中存储三条大河流及其流经的国家。其中一个键 — 值对可能是 'nile': 'egypt' 。 使用循环为每条河流打印一条消息,如“The Nileruns throughEgypt.” 。 使用循环将该字典中每条河流的名字都打印出来。 使用循环将该字典包含的每个国家的名字都打印出来。
rivers = {'nile': 'egypt', 'huanghe': 'china', 'rhine': 'Switzerland', } for key, value in rivers.items(): print("The " + key + " through " + value + ".")
The nile through egypt. The huanghe through china. The rhine through Switzerland. Process finished with exit code 06-6 调查: 在 6.3.1 节编写的程序 favorite_languages.py 中执行以下 *** 作。 创建一个应该会接受调查的人员名单,其中有些人已包含在字典中,而其他人未包含在字典中。 遍历这个人员名单,对于已参与调查的人,打印一条消息表示感谢。对于还未参与调查的人,打印一条消息邀请他参与调查。
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi', } user_1 = { 'name1': 'efermi', 'name2': 'Tom', 'name3': 'tonny' } for v in user_1.values(): if v in user_0.values(): print(v.title() + ", thanks for join!") else: print(v.title() + ", hope you join in our investigation.")
Efermi, thanks for join! Tom, hope you join in our investigation. Tonny, hope you join in our investigation. Process finished with exit code 06-7 人: 在为完成练习 6-1 而编写的程序中,再创建两个表示人的字典,然后将这三个字典都存储在一个名为 people 的列表中。遍历这个列表,将其中每个人的所有 信息都打印出来。
people = { 'people1': { 'first_name': 'Smith', 'last_name': 'Tom', 'age': '18', 'city': 'Shanghai', }, 'people2': { 'first_name': 'Blane', 'last_name': 'Sam', 'age': '22', 'city': 'Anshan', }, 'people3': { 'first_name': 'Ho', 'last_name': 'Jin', 'age': '25', 'city': 'Chongqing', }, } for person, peopleA in people.items(): print("nUsername: " + person) full_name = peopleA['first_name'] + " "+ peopleA['last_name'] age = peopleA['age'] city = peopleA['city'] print("t Full_name: " + full_name.title()) print("t Age: " + age) print("t City:" + city)
Username: people1 Full_name: Smith Tom Age: 18 City:Shanghai Username: people2 Full_name: Blane Sam Age: 22 City:Anshan Username: people3 Full_name: Ho Jin Age: 25 City:Chongqing Process finished with exit code 06-8 宠物: 创建多个字典,对于每个字典,都使用一个宠物的名称来给它命名;在每个字典中,包含宠物的类型及其主人的名字。将这些字典存储在一个名为 pets 的列表中,再遍历该列表,并将宠物的所有信息都打印出来。 【方法一】
Doggy_Sam = { 'kind': 'dog', 'owner': 'Tommy' } Cat_Jenny = { 'kind': 'cat', 'owner': 'tina' } Lion_Dazhuang = { 'kind': 'lion', 'owner': 'Jin' } Pets = [Doggy_Sam, Cat_Jenny, Lion_Dazhuang] for pet in Pets: print(pet)
{'kind': 'dog', 'owner': 'Tommy'} {'kind': 'cat', 'owner': 'tina'} {'kind': 'lion', 'owner': 'Jin'} Process finished with exit code 0【方法二】
Doggy_Sam = { 'kind': 'dog', 'owner': 'Tommy' } Cat_Jenny = { 'kind': 'cat', 'owner': 'tina' } Lion_Dazhuang = { 'kind': 'lion', 'owner': 'Jin' } Pets = [Doggy_Sam, Cat_Jenny, Lion_Dazhuang] for pet in Pets: if pet == Doggy_Sam: print( 'nDoggy_Sam:' + 'ntkind:' + pet['kind'] + 'ntowner:' + pet['owner'] ) elif pet == Cat_Jenny: print( 'nCat_Jenny:' + 'ntkind:' + pet['kind'] + 'ntowner:' + pet['owner'] ) elif pet == Lion_Dazhuang: print( 'nLion_Dazhuang:' + 'ntkind:' + pet['kind'] + 'ntowner:' + pet['owner'] )
Doggy_Sam: kind:dog owner:Tommy Cat_Jenny: kind:cat owner:tina Lion_Dazhuang: kind:lion owner:Jin Process finished with exit code 06-9 喜欢的地方: 创建一个名为 favorite_places 的字典。在这个字典中,将三个人的名字用作键;对于其中的每个人,都存储他喜欢的 1~3 个地方。为让这个练 习更有趣些,可让一些朋友指出他们喜欢的几个地方。遍历这个字典,并将其中每个人的名字及其喜欢的地方打印出来。
favorite_places = { 'Tom': ['Shanghai', 'Chongqing', 'Shengyang'], 'Tina':['Zhongshan', 'Beijing', 'London'], 'Tony':['Hangzhou', 'Xiaoshan', 'Guiyang'] } # 输出方式1: for name, place in favorite_places.items(): print(name + " would like to travel these places:", place) # 输出方式2: for name, place in favorite_places.items(): print(f"n{name.title()} likes to travel to these places:") for pla in place: print(pla)
Tom would like to travel these places: ['Shanghai', 'Chongqing', 'Shengyang'] Tina would like to travel these places: ['Zhongshan', 'Beijing', 'London'] Tony would like to travel these places: ['Hangzhou', 'Xiaoshan', 'Guiyang'] Tom likes to travel to these places: Shanghai Chongqing Shengyang Tina likes to travel to these places: Zhongshan Beijing London Tony likes to travel to these places: Hangzhou Xiaoshan Guiyang Process finished with exit code 06-10 喜欢的数字: 修改为完成练习 6-2 而编写的程序,让每个人都可以有多个喜欢的数字,然后将每个人的名字及其喜欢的数字打印出来。
numbers = { 'Tom': [5, 6, 68], 'Hu': [87, 55,45], 'Sui': [15, 22], 'Jin': [16,13], 'Bao': [666, 34, 34, 55] } for name, number in numbers.items(): print(f"n{name.title()} likes these number:") for num in number: print(num)
Tom likes these number: 5 6 68 Hu likes these number: 87 55 45 Sui likes these number: 15 22 Jin likes these number: 16 13 Bao likes these number: 666 34 34 55 Process finished with exit code 06-11 城市: 创建一个名为 cities 的字典,其中将三个城市名用作键;对于每座城市,都创建一个字典,并在其中包含该城市所属的国家、人口约数以及一个有关该 城市的事实。在表示每座城市的字典中,应包含 country 、 population 和 fact 等键。将每座城市的名字以及有关它们的信息都打印出来。
cities = { 'Chongqing': { 'country':'China', 'popilation':'100,000', 'description':'Chongqing is a beautiful mountain city' }, 'Shanghai': { 'country':'China', 'popilation': '900,000', 'description':'Shanghai is a beautiful modern city' }, 'Berlin': { 'country':'Deutschland', 'popilation':'985,154,454', 'description':'Berlin is the capital of Germany' }, } # 方式1: for city, info in cities.items(): print(f"n{city}:{info} ") # 方式2: for city, value in cities.items(): print(f"n{city}: ") for cityA, valueA in value.items(): print(f"{cityA}:{valueA}")
Chongqing:{'country': 'China', 'popilation': '100,000', 'description': 'Chongqing is a beautiful mountain city'} Shanghai:{'country': 'China', 'popilation': '900,000', 'description': 'Shanghai is a beautiful modern city'} Berlin:{'country': 'Deutschland', 'popilation': '985,154,454', 'description': 'Berlin is the capital of Germany'} Chongqing: country:China popilation:100,000 description:Chongqing is a beautiful mountain city Shanghai: country:China popilation:900,000 description:Shanghai is a beautiful modern city Berlin: country:Deutschland popilation:985,154,454 description:Berlin is the capital of Germany Process finished with exit code 06-12 扩展: 本章的示例足够复杂,可以以很多方式进行扩展了。请对本章的一个示例进行扩展:添加键和值、调整程序要解决的问题或改进输出的格式。 暂无后面有了更新
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)