ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 12 Jun 2012 22:17:33 +0200Variable assignment in listhttps://ask.sagemath.org/question/9064/variable-assignment-in-list/This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session:
sage: var('A')
A
sage: var('B')
B
sage: for item in alist:
item = B
print item
....:
B
sage: for item in alist:
print item
....:
A
I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?
EDIT: I should explain that what I want to do is not just change the items in the list, so I don't want to just make alist conatin the variable 'B'. I actually want to assign the value 'B' to the variable 'A', so that when I type 'A' into SAGE, the output is 'B'. This can be accomplished in this example with the one command:
sage: A = B
but the point is that if I have two lists
1. listVariables, e.g. [Y_1,...Y_n]
2. listExpressions, e.g. [X^2, X^3, Z^4, ..., XZ]
of unknown length and contents, except that the two lists have the same number of elements, I want to assign the value contained in listExpressions[i] to the variable in listVariables[i] by looping over the lists.heatkernelTue, 12 Jun 2012 22:17:33 +0200https://ask.sagemath.org/question/9064/