且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带副本的numpy数组分配

更新时间:2022-02-20 00:25:01

所有三个版本都做不同的事情:

All three versions do different things:

  1. B = A

这会将新名称B绑定到已经命名为A的现有对象.之后,它们引用相同的对象,因此,如果您就地修改一个对象,您也会在另一个对象中看到更改.

This binds a new name B to the existing object already named A. Afterwards they refer to the same object, so if you modify one in place, you'll see the change through the other one too.

B[:] = A(与B[:]=A[:]一样?)

这会将A中的值复制到现有数组B中.两个数组必须具有相同的形状才能起作用. B[:] = A[:]做同样的事情(但B = A[:]做的事情更像1).

This copies the values from A into an existing array B. The two arrays must have the same shape for this to work. B[:] = A[:] does the same thing (but B = A[:] would do something more like 1).

numpy.copy(B, A)

这不是合法语法.您可能是说B = numpy.copy(A).这几乎与2相同,但是它创建了一个新数组,而不是重用B数组.如果没有其他引用以前的B值,则最终结果将与2相同,但是在复制期间它将临时使用更多内存.

This is not legal syntax. You probably meant B = numpy.copy(A). This is almost the same as 2, but it creates a new array, rather than reusing the B array. If there were no other references to the previous B value, the end result would be the same as 2, but it will use more memory temporarily during the copy.

或者您的意思是numpy.copyto(B, A),它合法,等于2?

Or maybe you meant numpy.copyto(B, A), which is legal, and is equivalent to 2?