zoukankan      html  css  js  c++  java
  • python元组

    In [8]: t = (1,2, [30, 40])
    
    In [9]: t[2]
    Out[9]: [30, 40]
    
    In [10]: t[2] += [50,60]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-10-fb586dd7f384> in <module>()
    ----> 1 t[2] += [50,60]
    
    TypeError: 'tuple' object does not support item assignment
    
    In [11]: t
    Out[11]: (1, 2, [30, 40, 50, 60])
    
    In [12]: t[2]
    Out[12]: [30, 40, 50, 60]
    
    In [13]: t[2].append(70)
    
    In [14]: t
    Out[14]: (1, 2, [30, 40, 50, 60, 70])

    看下list的append和+=

    # test.py
    a = [1, 2, 3]
    a.append(4)
    a += [5]
    a = a + [6]
    E:workspace	utorial>python -m dis test.py
      1           0 LOAD_CONST               0 (1)
                  3 LOAD_CONST               1 (2)
                  6 LOAD_CONST               2 (3)
                  9 BUILD_LIST               3
                 12 STORE_NAME               0 (a)
    
      2          15 LOAD_NAME                0 (a)
                 18 LOAD_ATTR                1 (append)
                 21 LOAD_CONST               3 (4)
                 24 CALL_FUNCTION            1
                 27 POP_TOP
    
      3          28 LOAD_NAME                0 (a)
                 31 LOAD_CONST               4 (5)
                 34 BUILD_LIST               1
                 37 INPLACE_ADD
                 38 STORE_NAME               0 (a)
    
      4          41 LOAD_NAME                0 (a)
                 44 LOAD_CONST               5 (6)
                 47 BUILD_LIST               1
                 50 BINARY_ADD
                 51 STORE_NAME               0 (a)
                 54 LOAD_CONST               6 (None)
                 57 RETURN_VALUE
  • 相关阅读:
    js检测对象中是否存在某个属性
    ES6 笔记
    DataSet 用法
    CommandBehavior.CloseConnection有何作用
    SqlDataReader
    Listview.Finditem()函数用法
    Instr()函数用法
    StringBuilder与StringBuffer的区别
    [DllImport("kernel32.dll")]使用
    extern用法
  • 原文地址:https://www.cnblogs.com/jachin/p/7397969.html
Copyright © 2011-2022 走看看