摘要: arr=[[1,2,3],[4,2,3],[5,2,3]]x2=sorted(arr)print 'sorted',x2print '-'*20for ar in arr: ar.append('trap')print 'arr',arrprint 'sorted',x2结果:>>> sorted [[1, 2, 3], [4, 2, 3], [5, 2, 3]]--------------------arr [[1, 2, 3, 'trap'], [4, 2, 3, 't 阅读全文
posted @ 2013-10-23 12:54 LisPythoniC 阅读(236) 评论(0) 推荐(0) 编辑
摘要: >>> def dupu(t): return t>>> t=[1,2,3]>>> s=dupu(t)>>> s[1, 2, 3]>>> t.append(4)>>> s[1, 2, 3, 4]本来只想改变t列表,结果s列表也发生相同变化了.根本原因在于变量s和t都是指向同一个list对象.如何避免这种情况,让s和t相互独立?那就要让dupu返回一个新的list对象.>>> def dupu(t): return [i for i in t]>>> t 阅读全文
posted @ 2013-10-23 12:14 LisPythoniC 阅读(138) 评论(0) 推荐(0) 编辑
摘要: import randomdef randomlist(n): return [random.randint(0,100) for i in range(n)]def isorted(arr,key=None,reverse=False): if len(arr)key(line): big.append(ar) else: small.append(ar) else: for ar in arr[1:]: if ar>line: big.append(ar) ... 阅读全文
posted @ 2013-10-23 00:11 LisPythoniC 阅读(286) 评论(0) 推荐(0) 编辑