(先学第四章)
对象拷贝
任务:
Python通常只是使用指向原对象的引用,并不是真正的拷贝。
解决方案:
>>> a = [1,2,3] >>> import copy >>> c = copy.copy(a) >>> a.append(4) >>> a [1, 2, 3, 4] >>> c [1, 2, 3]
任务:
通过操作和处理一个序列(或者其他可以迭代对象)中元素来创建一个新的列表
解决方案:
>>> thenewlist = [x+23 for x in range(5)] >>> thenewlist [23, 24, 25, 26, 27]
>>> thenewlist = [x+23 for x in range(9) if x > 5] >>> thenewlist [29, 30, 31]