Home  >  Q&A  >  body text

Asking about a list assignment problem in Python

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]

女神的闺蜜爱上我女神的闺蜜爱上我2686 days ago951

reply all(6)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:26:18

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

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-12 09:26:18

    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.

    reply
    0
  • 怪我咯

    怪我咯2017-06-12 09:26:18

    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

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-12 09:26:18

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

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

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:26:18

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

    reply
    0
  • typecho

    typecho2017-06-12 09:26:18

    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
    >>> 

    reply
    0
  • Cancelreply