Python中的tuple

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, '注册')

    

  

  

posted @ 2018-11-18 20:35  风掠丶幽兰  阅读(394)  评论(0编辑  收藏  举报