variable-assignment

Python multiple assignment issue (list) [duplicate]

此生再无相见时 提交于 2019-11-28 14:15:50
This question already has an answer here: Tuple unpacking order changes values assigned 4 answers >>> i = 1 >>> A = [3,4,-1,1] >>> A[A[i] - 1], A[i] = A[i], A[A[i] - 1] >>> A [3, 1, -1, 4] >>> A = [3,4,-1,1] >>> A[i], A[A[i] - 1] = A[A[i] - 1], A[i] >>> A [4, 1, -1, 1] I have question when I do assignment for multiple variables for a list. Like the example above, the assignment A[A[i] - 1], A[i] = A[i], A[A[i] - 1] is different from the assignment A[i], A[A[i] - 1] = A[A[i] - 1], A[i] I am really confused the inside calculation order in Python. Why the results are different? What's the best

How array type assignment works?

两盒软妹~` 提交于 2019-11-28 14:05:41
I have the following code: type PSuperListItem = ^TSuperListItem; TSuperListItem = record SubItems : array of String; Marked : Boolean; ImageIndex: Integer; end; TSuperListItems = array of PSuperListItem; TMyList = class(TCustomControl) public Items, ItemsX : TSuperListItems; procedure SwapItemLists; end; procedure TMyList.SwapItemLists; var tmp:TSuperListItems; begin tmp:=Items; Items:=ItemsX; ItemsX:=tmp; end; I want to know if I've done correctly the assingtions from SwapItemLists . What happends when I assign Items to tmp ? Will be created a new copy of Items or will be passed just the

Visual Basic: Variable that references another variable changes when original vairable changes

限于喜欢 提交于 2019-11-28 14:01:58
问题 Example: Dim a As Integer = 1 Dim b As Integer = a Console.WriteLine(b) a = 0 Console.WriteLine(b) Output: 1 0 For some reason changing a also changes b even though I am not actually changing b. Why does this happen, and how do I get around it. 回答1: Integer is a Value type so when you assign 'a' to 'b' a COPY is made. Further changes to one or the other will only affect that particular copy in its own variable: Module Module1 Sub Main() Dim a As Integer = 1 Dim b As Integer = a Console

Python: setting two variable values separated by a comma in python

我们两清 提交于 2019-11-28 13:58:58
What is the difference in python between doing: a, b = c, max(a, b) and a = c b = max(a, b) what does having the two variable assignments set on the same line do? Your two snippets do different things: try with a , b and c equal to 7 , 8 and 9 respectively. The first snippet sets the three variables to 9 , 8 and 9 . In other words, max(a, b) is calculated before a is assigned to the value of c . Essentially, all that a, b = c, max(a, b) does is push two values onto the stack; the variables a and b are then assigned to these values when they are popped back off. On the other hand, running the

Mass variable declaration and assignment in R?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 13:40:35
Apologies if this is a stupid question - but this my first attempt at using R , and i have ended up writting some code along the lines of: some <- vector('list', length(files)) thing <- vector('list', length(files)) and <- vector('list', length(files)) another <- vector('list', length(files)) thing <- vector('list', length(files)) Is there a nicer (DRY) way to do this in R ? To rephrase, I want to assign the same value to multiple variables at once (as per @Sven Hohenstein's answer) If you want to assign the same value to multiple variables at once, use this: some <- thing <- and <- another <-

An unset C pointer is not null

妖精的绣舞 提交于 2019-11-28 12:55:26
I am messing around with C pointers. When I compile and run the following code. Example 1: #include <stdio.h> int main() { int k; int *ptr; k = 555; if (ptr == NULL) { printf("ptr is NULL\n"); } else { printf("ptr is not NULL\n"); printf("ptr value is %d\n", *ptr); } printf("ptr address is %p\n", ptr); } I get the output: ptr is not NULL ptr value is 1 ptr address is 0x7fff801ace30 If I don't assign a value to k: Example 2: #include <stdio.h> int main() { int k; int *ptr; if (ptr == NULL) { printf("ptr is NULL\n"); } else { printf("ptr is not NULL\n"); printf("ptr value is %d\n", *ptr); }

uninitialized local variable 'j' used

你离开我真会死。 提交于 2019-11-28 11:51:31
Here is a section of some code I have. Im getting an error uninitialized local variable 'j' used and I dont see it. as far as I can tell it is being used. Can someone please help? float Calculate(Element ElmAry[30], Formula FormAry[30]) { int i; int j; float MoleWT = 0; float MoleSum = 0; char e1; char e2; char f1; char f2; for(i = 0; i < 30; i++) { f1 = FormAry[j].Element1; f2 = FormAry[j].ElementA; e1 = ElmAry[i].eN1; e2 = ElmAry[i].eN1; if(e1 == f1 && e2 == f2) { MoleWT = ElmAry[i].Weight * FormAry[j].Atom; MoleSum = MoleSum + MoleWT; j++; } } return MoleSum; } You haven't given j a value,

How to append a string value during assignment in Javascript?

会有一股神秘感。 提交于 2019-11-28 10:28:46
Hey i dont know if that is possible but i want to set a given variable in js by reference . What i want to do is, that each time i pass a string to the function addstring that the value of the textfield is added like += function addstring(string) { document.getElementById("string").value = string; // its a textfield } How can i do that? += works fine. var str = "stack"; str += "overflow"; console.log(str); //alert(str); Use firebug!! stackoverflow Javascript does not support passing parameters by reference. This link offers a good breakdown and some workarounds- Passing by Value or reference

Counting Rooms While Knowing Where Walls Are

梦想与她 提交于 2019-11-28 10:26:04
This question is on code for C++ builder 6. The bounty is interested in a standard C++ algorithm to solve the problem given a standardized input (see this for more information.) The txt file which also represents the data I have in an array: 1101 0110 1101 0110 1100 0101 0110 1110 1001 0110 1011 1010 1111 1010 1000 0101 0011 1110 1011 1110 1010 1011 1101 0101 0001 0101 0011 1011 Explanation of the txt: The numbers from the txt file are a 4-bit representation of the walls to a room, with a set bit representing a wall. The wall bits are in clockwise order starting with the most significant bit

The use of “this” in Java

瘦欲@ 提交于 2019-11-28 10:16:15
If I write the following class: public class Example { int j; int k; public Example(int j, int k) { j = j; k = k; } public static void main(String[] args) { Example exm = new Example(1,2); System.out.println(exm.j); System.out.println(exm.k); } } The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write: this.j = j; this.k = k; But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand