Python学习笔记(七)- 列表和字典(Lists and Dictionaries)

1.说出两种创建包含了5个整数0的列表的方法。
答:一个字面量(literal)表达式如 [0, 0, 0, 0, 0] 和一个重复(repetition)表达式如 [0] * 5 每个可以创建5个0的列表。事实上,你可以用一个空列表 [] ,然后建立起循环,每次迭代附加(append)一个0,使用语句 L.append(0)。一个列表推导式([0 for i in range(5)])在这也能起作用,但这比你为这个答案所需要做的工作还要多。

 

2.说出两种创建有两个键(键'a', 'b',每个相关的值都是0)的字典的方法。
答:一个字面量(literal)表达式如 {'a': 0, 'b': 0} 或者一系列的赋值操作如 D = {}, D['a'] = 0 以及 D['b'] = 0 将创建你预期的字典。你当然可以使用更新颖和更容易编写的 dict(a=0, b=0) 关键字形式,或者更灵活的 dict([('a', 0), ('b', 0)]) 键/值序列形式。或者,因为所有的值是相等的,你可以使用特殊形式 dict.fromkeys('ab', 0)。在Python 3.X 和 2.7 中,你可以使用一个字典推导式:{k:0 for k in 'ab'} ,不过,这可能有点过头了。

 

3.说出四种改变列表对象的操作。
答:append 和 extend 方法可以使列表增长(grow),sort 和 reverse 方法可以排序和反转列表,insert 方法能以某个偏移量(offset)把项(item)插入列表中,remove 和 pop 方法按值和位置从列表中删除,del 语句可以删除一个对象或者切片,而且索引和切片赋值语句可以取代一个对象或者整个部分。对于这个问题,可以从上述中任意挑选四种方式来回答。

 

4.说出四种改变字典对象的操作。
答:字典主要是通过分配给新的或现有的键来更改的,这会创建或更改表中的键条目。而且,del 语句删除键的条目,字典 update 方法把一个字典就地融合到另一个字典中,D.pop(key) 移除一个键然后返回它对应的值。字典中也有其他更奇异的改变方法在本章中没有介绍,例如 setdefault ;查看参考资源来获取更多细节。

 

5.为什么你可以用字典取代列表?
答:当数据被打上标签(例如,带有字段名【field name】的记录),字典通常表现地更好;列表最好适用于没有打上标签的项的集合(例如,文件目录中所有的文件)。字典查找通常也比搜索列表更快,尽管这可能因程序而异。

 

标注:转载《Learning Python 5th Edition》[奥莱理]

1. Name two ways to build a list containing five integer zeros.
2. Name two ways to build a dictionary with two keys, 'a' and 'b', each having an associated value of 0.
3. Name four operations that change a list object in place.
4. Name four operations that change a dictionary object in place.
5. Why might you use a dictionary instead of a list?

1. A literal expression like [0, 0, 0, 0, 0] and a repetition expression like [0] * 5 will each create a list of five zeros. In practice, you might also build one up with a loop that starts with an empty list and appends 0 to it in each iteration, with L.append(0). A list comprehension ([0 for i in range(5)]) could work here, too, but this is more work than you need to do for this answer.
2. A literal expression such as {'a': 0, 'b': 0} or a series of assignments like D = {}, D['a'] = 0, and D['b'] = 0 would create the desired dictionary. You can also use the newer and simpler-to-code dict(a=0, b=0) keyword form, or the more flexible dict([('a', 0), ('b', 0)]) key/value sequences form. Or, because all the values are the same, you can use the special form dict.fromkeys('ab', 0). In 3.X and 2.7, you can also use a dictionary comprehension: {k:0 for k in 'ab'}, though again, this may be overkill here.
3. The append and extend methods grow a list in place, the sort and reverse methods order and reverse lists, the insert method inserts an item at an offset, the remove and pop methods delete from a list by value and by position, the del statement deletes an item or slice, and index and slice assignment statements replace an item or entire section. Pick any four of these for the quiz.
4. Dictionaries are primarily changed by assignment to a new or existing key, which creates or changes the key’s entry in the table. Also, the del statement deletes a key’s entry, the dictionary update method merges one dictionary into another in place, and D.pop(key) removes a key and returns the value it had. Dictionaries also have other, more exotic in-place change methods not presented in this chapter, such as setdefault; see reference sources for more details.
5. Dictionaries are generally better when the data is labeled (a record with field names, for example); lists are best suited to collections of unlabeled items (such as all the files in a directory). Dictionary lookup is also usually quicker than searching a list, though this might vary per program.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值