也许,您正在寻找这样的东西:
def mydeepcopy(L):
if isinstance(L, list):
ret = []
for i in L:
ret.append(mydeepcopy(i))
elif isinstance(L, (int, float, type(None), str, bool)):
ret = L
else:
raise ValueError("Unexpected type for mydeepcopy function")
return ret
lst = [ [1,2,3], [4,5,6], [7,8,9] ]
copy = mydeepcopy(lst)
print("Original Data: %s" % (lst))
print("Original IDs: %s = %s" % (id(lst),[ id(i) for i in lst ]))
print("Copied Data: %s" % (copy))
print("Copied IDs: %s = %s" % (id(copy),[ id(i) for i in copy ]))
可能的输出:
Original Data: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Original IDs: 140637274065288 = [140637274064712, 140637274064776, 140637274065352]
Copied Data: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copied IDs: 140637274065480 = [140637274065416, 140637274065160, 140637274065096]