简体   繁体   English

如何在不更改 python 中的原始值的情况下将二维数组中的元素分配给新变量?

[英]How to assign an element from a 2D array to a new variable without changing its original value in python?

I want to take a value from a 2D array (so this value has a type of array) and assign it to a new variable.我想从 2D 数组中获取一个值(因此该值具有数组类型)并将其分配给一个新变量。 But when I make changes to this new variable, its original value in that 2D array also changes.但是当我对这个新变量进行更改时,它在该二维数组中的原始值也会发生变化。 I want to know if there is a correct way that I can do this?我想知道是否有正确的方法可以做到这一点?

Here is my example code.这是我的示例代码。

a = [[1],[2],[3]]
b = a[1]
b.append(2)
a.append(b)
print(a)
print(b)

In the output, a, b will be在 output 中,a、b 将是

a = [[1], [2, 2], [3], [2, 2]]
b = [2, 2]

Where I don't want a[1] to be changed as well.我不希望 a[1] 也被更改。 If someone can help me figure this out?如果有人可以帮我解决这个问题? Thanks.谢谢。

This is probably one of the most confusing concept for beginner, when you are doing subscription slicing, b = a[1] , the reference to first array a has been passed to b , which you whatever action you do to b , will be done to a[1] .对于初学者来说,这可能是最令人困惑的概念之一,当您进行订阅切片时, b = a[1] ,对第一个数组a的引用已传递给b ,您对b执行的任何操作都将完成到a[1] unless you make a complete copy of the original array, any action to subscripted will lead to change in original , therefore, whenever you want to work with array without changing its initial value, copy it manually.除非您制作原始数组的完整副本,否则对下标的任何操作都会导致 original 发生变化,因此,无论何时您想在不更改其初始值的情况下使用数组,请手动复制它。

a = [[1],[2],[3]]
# in your case it is nested so it will be a bit intricate 
ac = list(map(lambda x:list(map(lambda y:y, x)), a))
b = ac[1]
b.append(2)
a.append(b)
print(a)
print(b)
# [[1], [2], [3], [2, 2]]
# [2, 2]

Because a[1] is an array, passing it to b will pass it by reference and not by value .因为a[1]是一个数组,将它传递给b将通过引用而不是通过值传递它。 You can use您可以使用

b = a[1].copy()

to make a new instance of a[1] without effecting the original a[1] .在不影响原始a[1]的情况下a[1]的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不删除/更改原始元素及其值的情况下将列表/数组中元素的索引更改为另一个位置/索引 - How to change the index of an element in a list/array to another position/index without deleting/changing the original element and its value 如何为具有动态大小的2d数组的元素分配值? - How to assign value to an element of 2d array with dynamic size? 如何在不使用嵌套 for 循环的情况下根据其索引值划分 2d numpy 数组中的每个元素? - How to divide each element in a 2d numpy array in relation to its index value without the use of nested for loops? 如何在python中重新分配变量而不更改其ID? - How to re-assign a variable in python without changing its id? 如何在不知道其位置的情况下从 2D Numpy 数组中删除元素 - How to delete an element from a 2D Numpy array without knowing its position 如何在for循环中为二维数组赋值 - How to assign value to 2d array in for loop Python-如何在不更改原始值的情况下从类返回值 - Python - How to return value from the class without changing the original value 在二维数组列表(字典)中分配新值 - Assign new value in a 2d array list (Dict) 如何从 Python 中的二维数组中删除特定元素? - How to remove a specific element from a 2d array in Python? 更改二维数组元素的值会更改整个列 - Changing value of a 2D array element changes the complete column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM