码迷,mamicode.com
首页 > 编程语言 > 详细

Python中的tuple

时间:2018-11-18 21:04:34      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:imp   sign   pytho   font   port   error   结合   ons   直接   

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

    

  

  

Python中的tuple

标签:imp   sign   pytho   font   port   error   结合   ons   直接   

原文地址:https://www.cnblogs.com/HopenZhang/p/9979269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!