Python中的tuple

風掠丶幽蘭發表於2018-11-18

tuple_lst = [

  (`元祖容器可雜湊`,),

  (`元祖中的元素不可直接修改`,),

  (`元祖可迭代`,),

  (`查`,),

  (`練習`,),

]

 

元祖容器可雜湊

  >>>hash((1,))

  3430019387558

元祖中的元素不可直接修改

  >>>tu = (1, 2, 3, [4])

  >>>tu[-1].append(5)

  >>>tu

  (1, 2, 3, [4, 5])

  >>>tu[0] = 6

  TypeError: `tuple` object does not support item assignment

元祖可迭代

  >>>from collections import Iterable

  >>>isinstance(tuple(), Iterable)

  True

  >>>tu = (`a`, `b`, `c`, `d`)

  >>>tu[0]

  `a`

  >>>tu[:2]

  (`a`, `b`)

 

練習

  列舉,列表,元祖的結合練習

    >>> lst = [(`登陸`, `sign_in`), (`註冊`, `sign_up`)]

    >>> for index, item in enumerate(lst, 1):

    …    index, item[0]

    …

    (1, `登陸`)

    (2, `註冊`)

    

  

  

相關文章