Numpy-复制操作

Numpy的复制操作包括简单赋值(不复制数据)、view(创建视图,数据共享)和copy(完全复制数据)。简单赋值在Python中类似引用操作,不会产生新数据。view方法创建指向相同数据的新数组,修改view会影响原始数组。copy方法则生成数据副本,修改副本不会影响原始数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Numpy快速入门官方教程:
https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

复制操作

Numpy中复制操作有三种,分别总结。

=

Simple assignments make no copy of array objects or of their data.
这是高级语言如C#等引用类型,对a的引用加1。

这里写图片描述

view

The view method creates a new array object that looks ate the same data.
这个方法只是在栈上重新分配了一个新的地址,这个地址依然指向a地址指向的堆上的空间。

这里写图片描述

这也就表明 c 改变堆上的内容,a也会改变。

这里写图片描述

help(a.view)

Help on built-in function view:

view(...) method of numpy.ndarray instance
    a.view(dtype=None, type=None)

    New view of array with the same data.

    Parameters
    ----------
    dtype : data-type or ndarray sub-class, optional
        Data-type descriptor of the returned view, e.g., float32 or int16. The
        default, None, results in the view having the same data-type as `a`.
        This argument can also be specified as an ndarray sub-class, which
        then specifies the type of the returned object (this is equival
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值