NumPy 沒有復(fù)制

2021-11-06 17:33 更新

簡單賦值不會復(fù)制對象或其數(shù)據(jù)。

  1. >>> a = np.array([[ 0, 1, 2, 3],
  2. ... [ 4, 5, 6, 7],
  3. ... [ 8, 9, 10, 11]])
  4. >>> b = a # no new object is created
  5. >>> b is a # a and b are two names for the same ndarray object
  6. True

Python 將可變對象作為引用傳遞,因此函數(shù)調(diào)用不會進行復(fù)制。

  1. >>> def f(x):
  2. ... print(id(x))
  3. ...
  4. >>> id(a) # id is a unique identifier of an object
  5. 148293216 # may vary
  6. >>> f(a)
  7. 148293216 # may vary
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號