“我的变量改变了(虽然我不想要),一旦我改变了用来设置它的其他变量”

huangapple go评论171阅读模式
英文:

My variable changes (though I don't want to) once I change the other variable I used to set it up

问题

我有这两个数组

x = np.array([[1, 0],
       [0, 1],
       [0, 0],
       [0, 0],
       [0, 0]])
z = np.array([[0, 0],
       [0, 0],
       [1, 0],
       [0, 1],
       [0, 0]])

我想要交换这两个数组的第一列。

target = 0
tempx = x[:, target]
tempz = z[:, target]

但是一旦我这样做

z[:, target] = tempx

它改变了"tempz",尽管我除了"z"之外什么都没做!

为什么会发生这种情况,我该如何更改我的代码以便交换列?

我预期"tempz"和"tempx"不会发生变化,只是因为我改变了"z"和"x"。这是一个错误的假设吗?

英文:

I have these two arrays

x = np.array([[1, 0],
       [0, 1],
       [0, 0],
       [0, 0],
       [0, 0]])
z = np.array([[0, 0],
       [0, 0],
       [1, 0],
       [0, 1],
       [0, 0]])

and I wanted to switch the first column of these two.

target = 0
tempx = x[:, target]
tempz = z[:, target]
'''
but once I do this
'''
z[:, target] = tempx

it changed "tempz" although I did not do nothing with it except "z"!

Why is this happening and how can I change my code so that it switches columns?

I expected no change in "tempz" and "tempx" just because I changed "z" and "x". Was this a wrong assumption?

答案1

得分: 0

你可以使用 numpy.copy: https://numpy.org/doc/stable/reference/generated/numpy.copy.html 文档中还提到了你遇到的问题。你并没有复制数组,而是创建了一个对它的引用!因此,更改原始数组也会影响到引用。

英文:

You can use numpy.copy: https://numpy.org/doc/stable/reference/generated/numpy.copy.html the documentation also mentions the problem you're having. You're not copying the array, but creating a reference to it! So changing the original also alters the reference.

答案2

得分: 0

要切换两个数组的第一列而不影响彼此,您需要复制列。

import numpy as np

x = np.array([[1, 0],
              [0, 1],
              [0, 0],
              [0, 0],
              [0, 0]])
z = np.array([[0, 0],
              [0, 0],
              [1, 0],
              [0, 1],
              [0, 0]])

target = 0
tempx = np.copy(x[:, target])
tempz = np.copy(z[:, target])

z[:, target] = tempx
x[:, target] = tempz

print("x:")
print(x)
print("z:")
print(z)
英文:

To switch the first column of the two arrays without affecting each other, you need to make a copy of the columns.

import numpy as np

x = np.array([[1, 0],
              [0, 1],
              [0, 0],
              [0, 0],
              [0, 0]])
z = np.array([[0, 0],
              [0, 0],
              [1, 0],
              [0, 1],
              [0, 0]])

target = 0
tempx = np.copy(x[:, target])
tempz = np.copy(z[:, target])

z[:, target] = tempx
x[:, target] = tempz

print("x:")
print(x)
print("z:")
print(z)

答案3

得分: 0

在NumPy中,当你对数组进行切片时,它会返回原始数组的视图而不是创建副本。例如,下面的代码将返回对数组z的视图,当你对其中一个数组(tempz和z)进行更改时,原始数组将被修改。

tempz = z[:, target]

正如其他人已经指出的,如果你想只更改指定的数组,可以使用 'numpy.copy' 函数。它会创建一个与 'z' 独立的新数组,不会受到对 'z' 的任何修改的影响。

我发现 这篇 文章很有帮助。

英文:

In numpy, when you slice an array, it returns a view of the original array instead of creating a copy. For example, the below code will return the view of the z array and when you make a change to the either array (tempz and z), the original array will be modified.

tempz = z[:, target]

As other people have already pointed out, if you want to make change to only the specified array, you can use 'numpy.copy' function. It will create a new array which is independent of 'z' and will not affected by any modifications to the 'z'

I found this! article helpful.

huangapple
  • 本文由 发表于 2023年6月25日 16:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76549635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定