NumPy 没有复制

2021-11-06 17:33 更新

简单赋值不会复制对象或其数据。

  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 将可变对象作为引用传递,因此函数调用不会进行复制。

  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
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号