简体   繁体   English

如何在Python中为多个变量分配相同的值

[英]How to assign the same value to multiple variables in Python

I have a question about Python, which I am kinda new to. 我有一个关于Python的问题,我有点陌生。 Let's assume I want to assign a 5x5 matrix to 10 different variables. 假设我想将5x5矩阵分配给10个不同的变量。 I searched across the board, and what I found was this: 我进行了全面搜索,发现的是:

a, b, c, d, e = myMatrix

That is all good, but in Python, this means that when I change a, I also change the values of the other variables, because they all come down to the same memory adress if I got this correctly. 一切都很好,但是在Python中,这意味着当我更改a时,我也会同时更改其他变量的值,因为如果我正确理解这些变量,它们都将归结为相同的内存地址。

My question: Is there a fast way of assigning myMatrix to multiple Variables and giving each of them a unique memory adress? 我的问题:是否有一种将myMatrix分配给多个变量并为每个变量赋予唯一内存地址的快速方法? So that I can change myMatrix without changing a, b or c. 这样我就可以更改myMatrix而不更改a,b或c。 I do explicitly search for some kind of multi-assignment. 我确实明确地寻找某种形式的多重分配。

Thanks in advance! 提前致谢!

use the [copy] module 使用[复制]模块

>>> import copy
>>> new_matrix = copy.deepcopy(myMatrix)

As Burhan Khalid and juanchopanza have pointed out, what happens in your example will be different in, for example, 正如Burhan Khalid和juanchopanza所指出的,在您的示例中发生的情况将有所不同,例如,

  1. the case where "myMatrix" is actually an array of 5 values (in which case "a" will get the first value and "e" will get the last value), and “ myMatrix”实际上是由5个值组成的数组的情况(在这种情况下,“ a”将获得第一个值,而“ e”将获得最后一个值),以及
  2. the case where "myMatrix" is an instance of an Object (in which case "a" through "e" will each refer to the same object). “ myMatrix”是对象的实例的情况(在这种情况下,“ a”至“ e”将分别引用相同的对象)。

It sounds like you're thinking of case 2, and hoping for something like a macro which will automatically expand your single assignment statement (with a single Right Hand Side Value, whether Deep Copied or not) into 5 assignment statements, each with its own Left Hand Side, Right Hand Side, and Deep Copy. 听起来您正在考虑情况2,并希望找到类似宏的内容,该宏将自动将您的单个赋值语句(带有单个“右侧值”,无论是否深度复制)扩展为5个赋值语句,每个赋值语句都有自己的左侧,右侧和深度复印。

I don't know of any way to do this, and I would point out that: 我不知道有什么办法,我要指出:

  • When most OO languages encounter an assignment operation like yours with an Object on the Right Hand Side, the compiler/interpreter looks for a "copy constructor" for the class of the RHS Object, and uses it (if found) to generate the value (an Object reference) which is actually assigned to the LHS. 当大多数OO语言遇到像您这样的赋值操作,并且在右侧有一个Object时,编译器/解释器将为RHS Object的类寻找“复制构造函数”,并使用它(如果找到)来生成值(实际分配给LHS的对象引用)。 Can you even imagine what the syntax could look like for what you're describing, where the copy constructor is supposed to be called 5 times to yield 5 different Objects on the RHS, references to which are then assigned to five different variables on the LHS? 您甚至可以想象一下您所描述的语法是什么样的吗? 应该将复制构造函数调用5次以在RHS上产生5个不同的对象,然后将对它们的引用分配给LHS上的5个不同的变量? What could you possibly write in a single assignment statement that would make this intent clear? 您可以在一个赋值语句中写些什么,以使意图明确?

  • If you're writing code where Deep vs. Shallow copies will actually have an effect on behavior then IMHO you owe it to yourself and anyone else who has to read and maintain your code to make this obvious and explicit - like the answer from wong2, repeated 5 times (once for each of the 5 variables). 如果您在编写代码时,深层副本与浅层副本实际上会对行为产生影响,那么恕我直言,您应该将它归功于您自己以及必须阅读并维护您的代码以使这一点变得明显而明确的其他人-就像wong2的回答一样,重复5次(每个5个变量一次)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM