元组(tuple):
说明:元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
示例:
name_tuple1 = ('physics', 'chemistry', 1997, 2000)
name_tuple2 = (1, 2, 3, 4, 5 )
name_tuple3 = "a", "b", "c", "d"
基本操作:
- 索引
- 切片
- 循环
- 长度
- 包含
class tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. """ def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def __add__(self, *args, **kwargs): # real signature unknown """ Return self+value. """ pass def __contains__(self, *args, **kwargs): # real signature unknown """ Return key in self. """ pass def __eq__(self, *args, **kwargs): # real signature unknown """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): # real signature unknown """ Return getattr(self, name). """ pass def __getitem__(self, *args, **kwargs): # real signature unknown """ Return self[key]. """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __ge__(self, *args, **kwargs): # real signature unknown """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): # real signature unknown """ Return self>value. """ pass def __hash__(self, *args, **kwargs): # real signature unknown """ Return hash(self). """ pass def __init__(self, seq=()): # known special case of tuple.__init__ """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. # (copied from class doc) """ pass def __iter__(self, *args, **kwargs): # real signature unknown """ Implement iter(self). """ pass def __len__(self, *args, **kwargs): # real signature unknown """ Return len(self). """ pass def __le__(self, *args, **kwargs): # real signature unknown """ Return self<=value. """ pass def __lt__(self, *args, **kwargs): # real signature unknown """ Return self<value. """ pass def __mul__(self, *args, **kwargs): # real signature unknown """ Return self*value.n """ pass @staticmethod # known case of __new__ def __new__(*args, **kwargs): # real signature unknown """ Create and return a new object. See help(type) for accurate signature. """ pass def __ne__(self, *args, **kwargs): # real signature unknown """ Return self!=value. """ pass def __repr__(self, *args, **kwargs): # real signature unknown """ Return repr(self). """ pass def __rmul__(self, *args, **kwargs): # real signature unknown """ Return self*value. """ pass
常用功能:
索引示例:
name_tuple = ('one','two') print(name_tuple[0]) print(name_tuple[1]) 运行结果: one two
len()示例:
name_tuple = ('one','two') print(name_tuple[len(name_tuple)-1]) 运行结果: two
切片示例:
name_tuple = ('one','two') print(name_tuple[0:1]) 运行结果: ('one',)
for循环示例:
name_tuple = ('one','two') for t in name_tuple: print(t) 运行结果: one two
del不支持(不可修改)。
元组内部提供的其他功能:
count(self, value):
说明:计算元组中某个元素出现的次数。value:要统计的元素。(返回int)
示例:
name_tuple = ('one','two','one') n = name_tuple.count('one') print(n) 运行结果: 2
index(self, value, start=None, stop=None):
说明:从元组中找出某个值第一个匹配项的索引位置。value:查找匹配项。start:开始位置(从0开始),默认为无。end:结束位置,默认为无。(返回int)
示例:
name_tuple = ('one','two','one') i = name_tuple.index('one') print(i) 运行结果: 0