串列元組互換

  1. 需要將元組改成串列時,可用 list() 函式,反之使用 tuple() 函式。將元組 tuplex 轉成串列 listx,內容就可變動了。
    [dywang@deyu zzz]$ cat tuple5.py
    #!/usr/bin/python3
    # coding: utf-8
    
    tuplex = ( 32, 12, 'abc' )
    listx = list(tuplex)
    print("listx =", listx)
    listx[1] = 99
    print("listx =", listx)
    
  2. 執行結果:元組轉成串列後,第 2 個元素由 12 變更成 99。
    [dywang@deyu zzz]$ ./tuple5.py 
    listx = [32, 12, 'abc']
    listx = [32, 99, 'abc']