-1
當我使用[:]是它的值的副本,而不使用它是參考和更改值?簡單arr1 = arr2和arr1 = arr2 [:]是什麼區別?
當我使用[:]是它的值的副本,而不使用它是參考和更改值?簡單arr1 = arr2和arr1 = arr2 [:]是什麼區別?
這關鍵取決於切片的對象。 經常,[:]
給你一個淺拷貝:
a = [1, 2, 3]
b = a[:]
# `b` is distinct from `a`, but `b` references the same objects that `a` does.
print(b is a) # False
print(all(x is y for x, y in zip(a, b)) # True
但它很容易寫一個病態的對象,它只是返回的對象本身:
class SliceMe(object):
def __getitem__(self, obj):
return self
@ TigerhawkT3 - 我想這可能是一個更好的因爲OP的問題並不一定是特定的列表:http://stackoverflow.com/q/6167238/748858 – mgilson
@mgilson - \ *聳肩\ *當然,對我來說似乎很好。隨意打開並重新關閉,如果你喜歡。 – TigerhawkT3