元组操作

£神魔★判官ぃ 2021-10-18 21:32 531阅读 0赞

元组是一种序列,它与列表一样,唯一不同的是列表是可修改的,而元组不可以,元组的创建也是较为简单的。

1、元组创建

  • 直接使用逗号、圆括号

    t=(1,2,3)
    print(type(t))#

  • 使用关键字tuple

ContractedBlock.gif ExpandedBlockStart.gif

  1. class tuple(object):
  2. """
  3. tuple() -> empty tuple
  4. tuple(iterable) -> tuple initialized from iterable's items
  5. If the argument is a tuple, the return value is the same object.
  6. """
  7. def count(self, value): # real signature unknown; restored from __doc__
  8. """ T.count(value) -> integer -- return number of occurrences of value """
  9. return 0
  10. def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  11. """
  12. T.index(value, [start, [stop]]) -> integer -- return first index of value.
  13. Raises ValueError if the value is not present.
  14. """
  15. return 0
  16. def __add__(self, *args, **kwargs): # real signature unknown
  17. """ Return self+value. """
  18. pass
  19. def __contains__(self, *args, **kwargs): # real signature unknown
  20. """ Return key in self. """
  21. pass
  22. def __eq__(self, *args, **kwargs): # real signature unknown
  23. """ Return self==value. """
  24. pass
  25. def __getattribute__(self, *args, **kwargs): # real signature unknown
  26. """ Return getattr(self, name). """
  27. pass
  28. def __getitem__(self, *args, **kwargs): # real signature unknown
  29. """ Return self[key]. """
  30. pass
  31. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  32. pass
  33. def __ge__(self, *args, **kwargs): # real signature unknown
  34. """ Return self>=value. """
  35. pass
  36. def __gt__(self, *args, **kwargs): # real signature unknown
  37. """ Return self>value. """
  38. pass
  39. def __hash__(self, *args, **kwargs): # real signature unknown
  40. """ Return hash(self). """
  41. pass
  42. def __init__(self, seq=()): # known special case of tuple.__init__
  43. """
  44. tuple() -> empty tuple
  45. tuple(iterable) -> tuple initialized from iterable's items
  46. If the argument is a tuple, the return value is the same object.
  47. # (copied from class doc)
  48. """
  49. pass
  50. def __iter__(self, *args, **kwargs): # real signature unknown
  51. """ Implement iter(self). """
  52. pass
  53. def __len__(self, *args, **kwargs): # real signature unknown
  54. """ Return len(self). """
  55. pass
  56. def __le__(self, *args, **kwargs): # real signature unknown
  57. """ Return self<=value. """
  58. pass
  59. def __lt__(self, *args, **kwargs): # real signature unknown
  60. """ Return self<value. """
  61. pass
  62. def __mul__(self, *args, **kwargs): # real signature unknown
  63. """ Return self*value.n """
  64. pass
  65. @staticmethod # known case of __new__
  66. def __new__(*args, **kwargs): # real signature unknown
  67. """ Create and return a new object. See help(type) for accurate signature. """
  68. pass
  69. def __ne__(self, *args, **kwargs): # real signature unknown
  70. """ Return self!=value. """
  71. pass
  72. def __repr__(self, *args, **kwargs): # real signature unknown
  73. """ Return repr(self). """
  74. pass
  75. def __rmul__(self, *args, **kwargs): # real signature unknown
  76. """ Return self*value. """
  77. pass

tuple类

  1. t=tuple([1,2,3])
  2. print(type(t))#<class 'tuple'>

注意:如果创建一个元素的元组,后面需要加逗号。

2、元组操作

之前说过元组是一种序列,那么序列的所有特性元组都是可以使用的。那么序列有哪些特性呢?

  • 索引取值
  • 分片
  • 序列相加
  • 序列乘法
  • in运算符
  • 序列内建函数

    索引、切片取值

    t=(1,2,3,4,5,)
    print(t[1:3])#(2, 3)

    元组相加

    t=(1,2,3,4,5,)
    t1=(‘hello’,)
    print(t+t1)#(1, 2, 3, 4, 5, ‘hello’)

    元组乘法

    t=(1,2,3,4,5,)
    print(t*2)#(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

    in运算符

    t=(1,2,3,4,5,)
    print(3 in t)#True

    内建函数

    t=(1,2,3,4,5,)
    print(len(t))#5
    print(min(t))#1
    print(max(t))#5

转载于:https://www.cnblogs.com/shenjianping/p/11002139.html

发表评论

表情:
评论列表 (有 0 条评论,531人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python (Tuple)操作

    Python的元组与列表类似,不同之处在于元组的元素不能修改,元组使用小括号,列表使用方括号,元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可 一、创建元组 代码

    相关 Python精通-Python操作

    > 导语 >   在介绍元组之前那,先来回顾一个变量赋值问题。对于字符串来说一旦被创建如果需要进行修改的时候就如同下图中展示的一样,需要新开辟空间对新的值进行存储。对于其他

    相关

    > 本质:有序的集合 > 特点: 1.跟列表相似 > 2.不可以修改 > 3.元组以小 () 包裹 > 元组是以 小 () 包裹,元素以 逗号 ,分隔 > 格

    相关 Python操作

    1、任务简介 我在之前的博客[《Python列表(“数组”)操作》][Python]中讲解了Python列表的一些主要操作方法,由于元组与列表有许多相同之处,所以今天就把

    相关 操作

    元组是一种序列,它与列表一样,唯一不同的是列表是可修改的,而元组不可以,元组的创建也是较为简单的。 1、元组创建 直接使用逗号、圆括号 t=(1,2,3)

    相关 python3 tuple 操作

    元组被称为只读列表,数据可被查询,但不能被修改,类似于列表的切片操作,元组写在小括号里面()元素之前用逗号隔开,对于一些不想被修改的数据,可以用元组来保存 创建元组 1