from copy import deepcopy
class old_class:def __init__(self):self.blah = 'blah'
class new_class(object):def __init__(self):self.blah = 'blah'
dignore = {str: None, unicode: None, int: None, type(None): None}
def Copy(obj, use_deepcopy=True):t = type(obj)
if t in (list, tuple):if t == tuple:# Convert to a list if a tuple to# allow assigning to when copyingis_tuple = Trueobj = list(obj)else:# Otherwise just do a quick slice copyobj = obj[:]is_tuple = False
# Copy each item recursivelyfor x in xrange(len(obj)):if type(obj[x]) in dignore:continueobj[x] = Copy(obj[x], use_deepcopy)
if is_tuple:# Convert back into a tuple againobj = tuple(obj)
elif t == dict:# Use the fast shallow dict copy() method and copy any# values which aren't immutable (like lists, dicts etc)obj = obj.copy()for k in obj:if type(obj[k]) in dignore:continueobj[k] = Copy(obj[k], use_deepcopy)
elif t in dignore:# Numeric or string/unicode?# It's immutable, so ignore it!pass
elif use_deepcopy:obj = deepcopy(obj)return obj
if __name__ == '__main__':import copyfrom time import time
num_times = 100000L = [None, 'blah', 1, 543.4532,['foo'], ('bar',), {'blah': 'blah'},old_class(), new_class()]
t = time()for i in xrange(num_times):Copy(L)print 'Custom Copy:', time()-t
t = time()for i in xrange(num_times):Copy(L, use_deepcopy=False)print 'Custom Copy Only Copying Lists/Tuples/Dicts (no classes):', time()-t
t = time()for i in xrange(num_times):copy.copy(L)print 'copy.copy:', time()-t
t = time()for i in xrange(num_times):copy.deepcopy(L)print 'copy.deepcopy:', time()-t
t = time()for i in xrange(num_times):L[:]print 'list slicing [:]:', time()-t
t = time()for i in xrange(num_times):list(L)print 'list(L):', time()-t
t = time()for i in xrange(num_times):[i for i in L]print 'list expression(L):', time()-t
t = time()for i in xrange(num_times):a = []a.extend(L)print 'list extend:', time()-t
t = time()for i in xrange(num_times):a = []for y in L:a.append(y)print 'list append:', time()-t
t = time()for i in xrange(num_times):a = []a.extend(i for i in L)print 'generator expression extend:', time()-t
from copy import deepcopy
a = [ [ list(range(1, 3)) for i in range(3) ] ]b = deepcopy(a)b[0][1]=[3]print('Deep:')print(a)print(b)print('-----------------------------')a = [ [ list(range(1, 3)) for i in range(3) ] ]b = a*1b[0][1]=[3]print('*1:')print(a)print(b)print('-----------------------------')a = [ [ list(range(1, 3)) for i in range(3) ] ]b = a[:]b[0][1]=[3]print('Vector copy:')print(a)print(b)print('-----------------------------')a = [ [ list(range(1, 3)) for i in range(3) ] ]b = list(a)b[0][1]=[3]print('List copy:')print(a)print(b)print('-----------------------------')a = [ [ list(range(1, 3)) for i in range(3) ] ]b = a.copy()b[0][1]=[3]print('.copy():')print(a)print(b)print('-----------------------------')a = [ [ list(range(1, 3)) for i in range(3) ] ]b = ab[0][1]=[3]print('Shallow:')print(a)print(b)print('-----------------------------')
x = [8, 6, 7, 5, 3, 0, 9]y = xfor index, element in enumerate(y):y[index] = element * 2# Expected result:# x = [8, 6, 7, 5, 3, 0, 9] <-- this is where the code is wrong.# y = [16, 12, 14, 10, 6, 0, 18]