数据类型--元祖

浅浅的花香味﹌ 2021-09-30 14:04 399阅读 0赞

元祖的概念

元组是另一个数据类型,类似于List(列表)。
元组用 “ (‘a’,2,’b’) “ 标识。内部元素用逗号隔开。
元祖内的元素不能二次赋值,相当于只读列表。
元祖、列表和字符串都是序列

序列的两个主要特点是:索引操作符&切片操作符
1、索引操作符让我们可以从序列中抓取一个特定项目。
2、切片操作符让我们能够获取序列的一个切片,即一部分序列。

  1. 1 1、想提取字母b,需要用索引
  2. 2 >>> str1='abcde'
  3. 3 >>> str1[1]
  4. 4 'b'
  5. 5
  6. 6 2、如果想取值到d,需要用切片
  7. 7 >>> str1[1:4]
  8. 8 'bcd'
  9. 9
  10. 10 注意:数字是可取的,但是':'是必须的

基本操作:

索引
切片
循环
长度
包含

  1. 1 #!/usr/bin/env python
  2. 2 # -*- coding:utf8 -*-
  3. 3
  4. 4
  5. 5 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
  6. 6 tinytuple = (123, 'john')
  7. 7
  8. 8 print(tuple) # 输出完整元组
  9. 9 print(tuple[0]) # 输出元组的第一个元素
  10. 10 print(tuple[1:3]) # 输出第二个至第三个的元素
  11. 11 print(tuple[2:]) # 输出从第三个开始至列表末尾的所有元素
  12. 12 print(tinytuple * 2) # 输出元组两次
  13. 13 print(tuple + tinytuple) # 打印组合的元组
  14. 14
  15. 15
  16. 16 返回结果:
  17. 17 ('abcd', 786, 2.23, 'john', 70.2)
  18. 18 abcd
  19. 19 (786, 2.23)
  20. 20 (2.23, 'john', 70.2)
  21. 21 (123, 'john', 123, 'john')
  22. 22 ('abcd', 786, 2.23, 'john', 70.2, 123, 'john')

元祖和列表的区别

  1. 1 #!/usr/bin/env python
  2. 2 # -*- coding:utf8 -*-
  3. 3
  4. 4
  5. 5 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
  6. 6 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
  7. 7
  8. 8 tuple[2] = 1000 # 元组中是非法应用
  9. 9
  10. 10 输出结果:
  11. 11 tuple[2] = 1000 # 元组中是非法应用
  12. 12 TypeError: 'tuple' object does not support item assignment
  13. 13
  14. 14
  15. 15 list[2] = 1000 # 列表中是合法应用
  16. 16
  17. 17 显示结果:
  18. 18 ['abcd', 786, 1000, 'john', 70.2]

元祖参数:

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 lass tuple(object):
  2. 2 """
  3. 3 tuple() -> empty tuple
  4. 4 tuple(iterable) -> tuple initialized from iterable's items
  5. 5
  6. 6 If the argument is a tuple, the return value is the same object.
  7. 7 """
  8. 8 def count(self, value): # real signature unknown; restored from __doc__
  9. 9 """ T.count(value) -> integer -- return number of occurrences of value """
  10. 10 return 0
  11. 11
  12. 12 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  13. 13 """
  14. 14 T.index(value, [start, [stop]]) -> integer -- return first index of value.
  15. 15 Raises ValueError if the value is not present.
  16. 16 """
  17. 17 return 0
  18. 18
  19. 19 def __add__(self, y): # real signature unknown; restored from __doc__
  20. 20 """ x.__add__(y) <==> x+y """
  21. 21 pass
  22. 22
  23. 23 def __contains__(self, y): # real signature unknown; restored from __doc__
  24. 24 """ x.__contains__(y) <==> y in x """
  25. 25 pass
  26. 26
  27. 27 def __eq__(self, y): # real signature unknown; restored from __doc__
  28. 28 """ x.__eq__(y) <==> x==y """
  29. 29 pass
  30. 30
  31. 31 def __getattribute__(self, name): # real signature unknown; restored from __doc__
  32. 32 """ x.__getattribute__('name') <==> x.name """
  33. 33 pass
  34. 34
  35. 35 def __getitem__(self, y): # real signature unknown; restored from __doc__
  36. 36 """ x.__getitem__(y) <==> x[y] """
  37. 37 pass
  38. 38
  39. 39 def __getnewargs__(self, *args, **kwargs): # real signature unknown
  40. 40 pass
  41. 41
  42. 42 def __getslice__(self, i, j): # real signature unknown; restored from __doc__
  43. 43 """
  44. 44 x.__getslice__(i, j) <==> x[i:j]
  45. 45
  46. 46 Use of negative indices is not supported.
  47. 47 """
  48. 48 pass
  49. 49
  50. 50 def __ge__(self, y): # real signature unknown; restored from __doc__
  51. 51 """ x.__ge__(y) <==> x>=y """
  52. 52 pass
  53. 53
  54. 54 def __gt__(self, y): # real signature unknown; restored from __doc__
  55. 55 """ x.__gt__(y) <==> x>y """
  56. 56 pass
  57. 57
  58. 58 def __hash__(self): # real signature unknown; restored from __doc__
  59. 59 """ x.__hash__() <==> hash(x) """
  60. 60 pass
  61. 61
  62. 62 def __init__(self, seq=()): # known special case of tuple.__init__
  63. 63 """
  64. 64 tuple() -> empty tuple
  65. 65 tuple(iterable) -> tuple initialized from iterable's items
  66. 66
  67. 67 If the argument is a tuple, the return value is the same object.
  68. 68 # (copied from class doc)
  69. 69 """
  70. 70 pass
  71. 71
  72. 72 def __iter__(self): # real signature unknown; restored from __doc__
  73. 73 """ x.__iter__() <==> iter(x) """
  74. 74 pass
  75. 75
  76. 76 def __len__(self): # real signature unknown; restored from __doc__
  77. 77 """ x.__len__() <==> len(x) """
  78. 78 pass
  79. 79
  80. 80 def __le__(self, y): # real signature unknown; restored from __doc__
  81. 81 """ x.__le__(y) <==> x<=y """
  82. 82 pass
  83. 83
  84. 84 def __lt__(self, y): # real signature unknown; restored from __doc__
  85. 85 """ x.__lt__(y) <==> x<y """
  86. 86 pass
  87. 87
  88. 88 def __mul__(self, n): # real signature unknown; restored from __doc__
  89. 89 """ x.__mul__(n) <==> x*n """
  90. 90 pass
  91. 91
  92. 92 @staticmethod # known case of __new__
  93. 93 def __new__(S, *more): # real signature unknown; restored from __doc__
  94. 94 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  95. 95 pass
  96. 96
  97. 97 def __ne__(self, y): # real signature unknown; restored from __doc__
  98. 98 """ x.__ne__(y) <==> x!=y """
  99. 99 pass
  100. 100
  101. 101 def __repr__(self): # real signature unknown; restored from __doc__
  102. 102 """ x.__repr__() <==> repr(x) """
  103. 103 pass
  104. 104
  105. 105 def __rmul__(self, n): # real signature unknown; restored from __doc__
  106. 106 """ x.__rmul__(n) <==> n*x """
  107. 107 pass
  108. 108
  109. 109 def __sizeof__(self): # real signature unknown; restored from __doc__
  110. 110 """ T.__sizeof__() -- size of T in memory, in bytes """
  111. 111 pass
  112. 112
  113. 113 tuple

元祖源代码

转载于:https://www.cnblogs.com/abobo/p/8032731.html

发表评论

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

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

相关阅读