复制的数组受到python中原始数组的更改的影响

问题描述 投票:0回答:1

jupyter笔记本中使用python,当我更改作为另一个数组副本的数组值时,它将影响原始数组。这对我来说不方便使用。

下面的代码我在jupyter笔记本上尝试过,并且我正在更改arr_temp [1]数组的值。但这会影响原始numpy数组

import numpy as np
array = np.array([1,5,6,7,8,94])
array[4:6]
arr_temp = array[4:6]
arr_temp[1]=100
array

我希望array([ 1, 5, 6, 7, 8, 94]),但我得到的值是array([ 1, 5, 6, 7, 8, 100])

python arrays numpy jupyter-notebook
1个回答
1
投票

尝试使用arr_temp = array[4:6].copy()。要更改数据的子集时,应始终使用copy(),否则python将其视为切片,并更改了新对象和原始对象。

© www.soinside.com 2019 - 2024. All rights reserved.