NumPy 視圖或淺拷貝

2021-11-06 17:33 更新

不同的數(shù)組對象可以共享相同的數(shù)據(jù),view方法創(chuàng)建一個查看相同數(shù)據(jù)的新數(shù)組對象。

  1. >>> c = a.view()
  2. >>> c is a
  3. False
  4. >>> c.base is a # c is a view of the data owned by a
  5. True
  6. >>> c.flags.owndata
  7. False
  8. >>>
  9. >>> c = c.reshape((2, 6)) # a's shape doesn't change
  10. >>> a.shape
  11. (3, 4)
  12. >>> c[0, 4] = 1234 # a's data changes
  13. >>> a
  14. array([[ 0, 1, 2, 3],
  15. [1234, 5, 6, 7],
  16. [ 8, 9, 10, 11]])

切片數(shù)組返回它的視圖:

  1. >>> s = a[:, 1:3]
  2. >>> s[:] = 10 # s[:] is a view of s. Note the difference between s = 10 and s[:] = 10
  3. >>> a
  4. array([[ 0, 10, 10, 3],
  5. [1234, 10, 10, 7],
  6. [ 8, 10, 10, 11]])
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號