ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

课后习题1

2019-07-15 21:02:08  阅读:749  来源: 互联网

标签:并打印 life animals things e2f cats 课后 习题


(1) 创建一个叫作years_list 的列表,存储从你出生的那一年到五岁那一年的年份。例
如,如果你是1980 年出生的,那么你的列表应该是years_list = [1980, 1981, 1982,
1983, 1984, 1985]。
如果你现在还没到五岁却在阅读本书,那我真的没有什么可教你的了。


1 >>> My_year_list = [1990,1991,1992,1993,1994,1995]
2 >>> My_year_list
3 [1990, 1991, 1992, 1993, 1994, 1995]

 

(2) 在years_list 中,哪一年是你三岁生日那年?别忘了,你出生的第一年算0 岁。

1 >>> My_year_list[3]
2 1993

 

(3) 在years_list 中,哪一年你的年纪最大?

 

1 >>> My_year_list[-1]
2 1995
3 >>> 

 

(4) 创建一个名为things 的列表, 包含以下三个元素:"mozzarella"、"cinderella" 和
"salmonella"。

>>> things = [ 'mozzarella', 'cinderella' , 'salmonella']
>>> things
['mozzarella', 'cinderella', 'salmonella']

 

(5) 将things 中代表人名的字符串变成首字母大写形式,并打印整个列表。看看列表中的

元素改变了吗?

1 >>> things = [ 'mozzarella', 'cinderella' , 'salmonella']
2 >>> things[things.index("cinderella")] = things[things.index("cinderella")].capitalize()
3 >>> things
4 ['mozzarella', 'Cinderella', 'salmonella']

(6) 将things 中代表奶酪的元素全部改成大写,并打印整个列表。

1 >>> things[things.index("mozzarella")] = things[things.index("mozzarella")].upper()
2 >>> things
3 ['MOZZARELLA', 'Cinderella', 'salmonella']
4 >>> 

(7) 将代表疾病的元素从things 中删除,收好你得到的诺贝尔奖,并打印列表。

1 >>> things.remove("salmonella")
2 >>> things
3 ['MOZZARELLA', 'Cinderella']
4 >>> 

(8) 创建一个名为surprise 的列表,包含以下三个元素:"Groucho"、"Chico" 和"Harpo"。

1 >>> surprise = [ 'Groucho','Chico','Harpo' ]
2 >>> surprise
3 ['Groucho', 'Chico', 'Harpo']
4 >>> 

(9) 将surprise 列表的最后一个元素变成小写,翻转过来,再将首字母变成大写。

1 >>> surprise[-1] = surprise[-1].lower()
2 >>> surprise
3 ['Groucho', 'Chico', 'harpo']
4 >>> 

(10) 创建一个名为e2f 的英法字典并打印出来。这里提供一些单词对:dog 是chien,cat

是chat,walrus 是morse。

1 >>> e2f = {
2     "dog":"chien",
3     "cat":"chat",
4     "walrus":"morse"
5     }
6 >>> e2f
7 {'dog': 'chien', 'cat': 'chat', 'walrus': 'morse'}

(11) 使用你的仅包含三个词的字典e2f 查询并打印出walrus 对应的的法语词。

1 >>> e2f["walrus"]
2 'morse'
3 >>> 

(12) 利用e2f 创建一个名为f2e 的法英字典。注意要使用items 方法。

  

1 f2e = { value:key for key,value in e2f.items() }
>>> f2e
{'chien': 'dog', 'chat': 'cat', 'morse': 'walrus'}

 

(13) 使用f2e,查询并打印法语词chien 对应的英文词。

1 >>> f2e["chien"]
2 'dog'

 

(14) 创建并打印由e2f 的键组成的英语单词集合。

1 >>> listA = [ e2f.keys() ]
2 >>> listA
3 [dict_keys(['dog', 'cat', 'walrus'])]

(15) 建立一个名为life 的多级字典。将下面这些字符串作为顶级键:'animals'、'plants'

以及'others'。令'animals' 键指向另一个字典,这个字典包含键'cats'、'octopi'
以及'emus'。令'cat' 键指向一个字符串列表,这个列表包括'Henri'、'Grumpy' 和
'Lucy'。让其余的键都指向空字典。

 1 >>> cat = [ 'Henri','Grumpy','Lucy' ]
 2 >>> octopi = {}
 3 >>> emus = {}
 4 >>> animals = {
 5     "cats":cat,
 6     "octopi":octopi,
 7     "emus":emus,
 8     }
 9 >>> animals
10 {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}
11 >>> plants={}
12 >>> others={}
13 >>> life = {
14     "animals":animals,
15     "plants":plants,
16     "others":others,
17     }
18 >>> life
19 {'animals': {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}, 'plants': {}, 'others': {}}
20 >>> print(life)
21 {'animals': {'cats': ['Henri', 'Grumpy', 'Lucy'], 'octopi': {}, 'emus': {}}, 'plants': {}, 'others': {}}
22 >>> 

(16) 打印life 的顶级键。

1 >>> life.keys()
2 dict_keys(['animals', 'plants', 'others'])

 

(17) 打印life['animals'] 的全部键。

1 >>> life["animals"].keys()
2 dict_keys(['cats', 'octopi', 'emus'])
3 >>> 

 

(18) 打印life['animals']['cats'] 的值。

1 >>> life["animals"]["cats"]
2 ['Henri', 'Grumpy', 'Lucy']
3 >>> print(life["animals"]["cats"])
4 ['Henri', 'Grumpy', 'Lucy']
5 >>> 

 

标签:并打印,life,animals,things,e2f,cats,课后,习题
来源: https://www.cnblogs.com/jingle007/p/11134834.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有