Asking about a list assignment problem in Python
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-06-12 09:24:18
0
6
981

Why

s = [1, 2, 3, 4, 5, 6]
i = 0
i = s[i] = 3
The result is: [1, 2, 3, 3, 5, 6] instead of [3, 2, 3, 4, 5, 6]

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(6)
Ty80

You can refer to an article I wrote below: Python: The Pitfalls of Chained Assignment

扔个三星炸死你

According to Assignment statements:

a = b = c = d = ... = E

is equivalent to

a = E
b = E
c = E
d = E
...

So: i=s[i]=3is equivalent to:

i = 3
s[i] = 3

The assignment in Python is a statement, not an operator, so the expression (a=b) will produce a syntax error, and the assignment statement has no value.

刘奇

Reference https://stackoverflow.com/que...

is equivalent to

s = [1, 2, 3, 4, 5, 6]
i = 0
temp_value = 3
i = temp_value
s[i] = temp_value

First, i becomes 3, and then the value s[i] is assigned

滿天的星座

Looking back at the results, i=3 was executed before s[i] = 3.

Can’t you just write it in two separate sentences?

仅有的幸福

You can use PythonTutor.com
i = s[i] = 3 That line basically executes i=3 and s[i]=3 successively

typecho

i = s[i] = 3 is equivalent to i = 3; s[i] = 3

Use dis module to analyze the execution process:

>>> def f():
    s = [1, 2, 3, 4, 5, 6]
    i = 0
    i = s[i] = 3

    
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 LOAD_CONST               4 (4)
             12 LOAD_CONST               5 (5)
             15 LOAD_CONST               6 (6)
             18 BUILD_LIST               6
             21 STORE_FAST               0 (s) # s = [1, 2, 3, 4, 5, 6]

  3          24 LOAD_CONST               7 (0)
             27 STORE_FAST               1 (i) # i = 0

  4          30 LOAD_CONST               3 (3) # 常量3 入栈
             33 DUP_TOP                        # 复制栈顶,也就是 常量3
             34 STORE_FAST               1 (i) # i = 3
             37 LOAD_FAST                0 (s)
             40 LOAD_FAST                1 (i)
             43 STORE_SUBSCR                   # s[i] = 3
             44 LOAD_CONST               0 (None) # 返回 None
             47 RETURN_VALUE

Example of separate writing

>>> def f2():
    s = [1, 2, 3, 4, 5, 6]
    i = 0
    i = 3
    s[i] = 3

    
>>> dis.dis(f2)
  2           0 LOAD_CONST               1 (1)
              3 LOAD_CONST               2 (2)
              6 LOAD_CONST               3 (3)
              9 LOAD_CONST               4 (4)
             12 LOAD_CONST               5 (5)
             15 LOAD_CONST               6 (6)
             18 BUILD_LIST               6
             21 STORE_FAST               0 (s) # s = [1, 2, 3, 4, 5, 6]

  3          24 LOAD_CONST               7 (0)
             27 STORE_FAST               1 (i) # i = 0

  4          30 LOAD_CONST               3 (3)
             33 STORE_FAST               1 (i) # i = 3

  5          36 LOAD_CONST               3 (3)
             39 LOAD_FAST                0 (s)
             42 LOAD_FAST                1 (i)
             45 STORE_SUBSCR                   # s[i] = 3
             46 LOAD_CONST               0 (None)
             49 RETURN_VALUE
>>> 
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template