+ 1
Output list
arr = [4,3,1,2] arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0] print(arr) Why output [2, 4,1,2] Why not [2, 3,1,4]
8 Respuestas
+ 3
Your question is similar to your previous question, but this time it is unique and interesting.
It is my best guess, step by step.
arr = [4, 3, 1, 2]
arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0]
arr[0], arr[arr[0]-1] = arr[4-1], 4
arr[0], arr[arr[0]-1] = arr[3], 4
arr[0], arr[arr[0]-1] = 2, 4
# value of arr[0] is reassigned
# arr = [2, 3, 1, 2]
# now arr[arr[0]-1] = 4 is left
arr[arr[0]-1] = 4
arr[2-1] = 4
arr[1] = 4
# arr = [2, 4, 1, 2]
My explanation is that the "shortest" variable get reassigned first (arr[0] on the left), then the new value will be used on the "longer" variable getting reassign (arr[arr[0]-1] on the left).
I’m not a expert, if anyone has a better explanation please share with us.
As a reference, I used <<https://pythontutor.com>> to help me make educated guess on this problem.
0
I think I made an incorrect answer to this question after viewing another question from @Buterbrod:)
https://www.sololearn.com/discuss/3245591/?ref=app
While the old theory fit the original question, it doesn't fit anymore if we shift the code around.
arr = [4, 3, 1, 2]
arr[arr[0]-2], arr[0] = arr[arr0]-1], arr[1]
print(arr)
With previous theory, it become:
arr[arr[0]-2], arr[0] = arr[4-1], 3
arr[arr[0]-2], arr[0] = arr[3], 3
arr[arr[0]-2], arr[0] = 2, 3
arr[0] got reassigned first and become 3, what left behind is:
arr[arr[0]-2] = 2
arr[3-2] = 2
arr[1] = 2
Following above steps the result should be [3, 2, 1, 2], but instead we got [3, 3, 2, 2]
My new theory is the code does reading from left to right, but work differently depends on how the variables are placed.
If a and b is going to be reassigned, 1) b is referencing a; 2) b is placed AFTER a, then the original theory stands because a got reassigned first.
ps. in original question, a is "arr[0]", and b is "arr[arr[0]-1]"
[1/2]
0
If a and b switch place the code is still reading from left to right. With the altered example:
arr = [4, 3, 1, 2]
arr[arr[0]-2], arr[0] = arr[arr[0]-1], arr[1]
arr[4-2], arr[0] = arr[4-1], 3
arr[2], arr[0], = arr[3], 3
arr[2], arr[0] = 2, 3
# [3, 3, 2, 2]
[2/2]
0
arr = [4,3,1,2]
arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0] # This is a multiple assignment
print(arr)
"""
In case of multiple assignment, values are passed to the right side of the expression after the assignment operator.
That is, we get the right part of the expression = arr[3], arr[0] => 2, 4
So we can safely write:
arr[0], arr[arr[0]-1] = 2, 4
(2, 4) – this is nothing but a tuple for which parentheses are not required and here the tuple is unpacked.
arr[0]=2 => arr[2-1]=4 => arr=[2,4,1,2]
"""
arr = [4,3,1,2]
arr[0], arr[arr[0]-1] = arr[3], 4
print(arr) # checking
arr = [4,3,1,2]
arr[0], arr[1] = 2, 4
print(arr) # checking
0
Solo, we understand it is a multiple assignment. What Антон Баранов doesn't understand is what the output is not [2, 3, 1, 4].
Below is the think leading to [2, 3, 1, 4]
arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0]
arr[0], arr[4-1] = arr[4-1], 4
arr[0], arr[3] = arr[3], 4
arr[0], arr[3] = 2, 4
>> [2, 3, 1, 4]
In your explanation the left x got reassigned to 2, and doesn't explain why the right x become arr[2-1].
The initial thought the right x is arr[4-1], becase arr[0] was 4.
0
Wong Hei Ming, unfortunately, you haven't read my explanation carefully: the values of the "arr" list are passed to the right side of the expression after the "=" operator, the left side of the expression takes values from the right side of the expression. I wrote the definitions of this expression so that you can Google and read the rules when performing "multiple assignment" and "unpacking a tuple".
Maybe it will be clearer to you this way:
arr = [4,3,1,2]
y = arr[0]
x = y - 1
x = arr[x]
| <<<<<<<<<<<<<< |
| >>>>>> | |
arr[0], arr[arr[0]-1] = x, y
print(arr) # [2,4,1,2]
0
Solo, I understand how 'multiple assignment' and 'unpacking tuple' wokrs.
Let's rewrite the expression wiht a, b, c and d to replace the confusing writing.
arr = [4, 3, 1, 2]
arr[0], arr[arr[0]-1] = arr[arr[0]-1], arr[0] turns into:
a, b = c, d
Because right side of "=" operator get evaluated first, we know c=arr[4-1]=arr[3]=2, and d=4.
It becomes a, b = 2, 4. I think we all agree on that.
The problem lies in 'b'. Why it is arr[1] but not arr[3], just like c is evaluated?
If the program run in parallel, shouldn't b=arr[4-1]=arr[3]?
My explanation is computer read the code left to right. 'a' got reassigned to 2 first, and 'b' is referencing 'a' (arr[0]), thus b=arr[2-1]=arr[1] and finally reassigned with 4.
However in your explanation I don't see how 'b' become arr[1], and >>print(arr) # checking<< doesn't help us to understand.
0
Wong Hei Ming, I believe that this should be absolutely clear, since I do not change the left part of the expression until the last moment, then it does not change either.
Your example a, b = c, d is incorrect, hence, in my opinion, all the confusion.
Should be a, b[a] = c, d