Python基本数据类型(元组)
Python基本数据类型(元组)
⼀、概述
元组(Tuple)是⼀种与列表类似的序列类型。元组的基本⽤法与列表⼗分类似。只不过元组⼀旦创建,就不能改变,因此,元组可以看成是⼀种不可变的列表。
⼆、元组格式
Python⽤⼀对括号“()”⽣成元组,中间的元素⽤逗号“,”隔开。尽量在最后元素后⾯加上⼀个额外的逗号“,”加以区分括号与元组,特别是只含单元素的元组:
tu = (11,22,"alex",[(33,44)],)
三、元组与列表的相互转换
列表和元组可以使⽤tuple()函数和list()函数相互转换:
li = [3,6,1,5,4,6]
print(tuple(li))        #结果为:(3, 6, 1, 5, 4, 6)
tu = (11,22,"alex",[(33,44)],)
print(list(tu))        #结果为:[11, 22, 'alex', [(33, 44)]]
四、索引和切⽚
对于元组来说,只能通过索引和切⽚来取值,不能进⾏修改操作。
tu = (11,22,"alex",[(33,44)],)
print(tu[3][0][1])      #结果为:44
print(tu[1:-1])      #结果为:(22, 'alex')
五、元组的⽅法
由于元组是不可变的,所有它只⽀持.count()⽅法和.index()⽅法,⽤法与列表⼀致:
.count()⽅法是计算元组的指定元素出现的次数。
tu = (11,22,"alex",[(33,44)],22,)
unt(22))    #结果为:2
.index()⽅法是获取指定元素第⼀次出现的索引位置。
tu = (11,22,"alex",[(33,44)],22,)
print(tu.index(22))      #结果为:1
元组所有⽅法归纳:
1 lass tuple(object):
2"""
3    tuple() -> empty tuple
4    tuple(iterable) -> tuple initialized from iterable's items
5
6    If the argument is a tuple, the return value is the same object.
7"""
8def count(self, value): # real signature unknown; restored from __doc__
9""" T.count(value) -> integer -- return number of occurrences of value """
10return 0
11
12def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
13"""
14        T.index(value, [start, [stop]]) -> integer -- return first index of value.
15        Raises ValueError if the value is not present.
16"""
17return 0
18
19def__add__(self, y): # real signature unknown; restored from __doc__
20""" x.__add__(y) <==> x+y """
21pass
22
23def__contains__(self, y): # real signature unknown; restored from __doc__
24""" x.__contains__(y) <==> y in x """
25pass
26
27def__eq__(self, y): # real signature unknown; restored from __doc__
28""" x.__eq__(y) <==> x==y """
29pass
30
31def__getattribute__(self, name): # real signature unknown; restored from __doc__ 32""" x.__getattribute__('name') <==> x.name """
33pass
34
35def__getitem__(self, y): # real signature unknown; restored from __doc__
getattribute方法返回类型
36""" x.__getitem__(y) <==> x[y] """
37pass
38
39def__getnewargs__(self, *args, **kwargs): # real signature unknown
40pass
41
42def__getslice__(self, i, j): # real signature unknown; restored from __doc__
43"""
44        x.__getslice__(i, j) <==> x[i:j]
45
46                  Use of negative indices is not supported.
47"""
48pass
49
50def__ge__(self, y): # real signature unknown; restored from __doc__
51""" x.__ge__(y) <==> x>=y """
52pass
53
54def__gt__(self, y): # real signature unknown; restored from __doc__
55""" x.__gt__(y) <==> x>y """
56pass
57
58def__hash__(self): # real signature unknown; restored from __doc__
59""" x.__hash__() <==> hash(x) """
60pass
61
62def__init__(self, seq=()): # known special case of tuple.__init__
63"""
64        tuple() -> empty tuple
65        tuple(iterable) -> tuple initialized from iterable's items
66
67        If the argument is a tuple, the return value is the same object.
68        # (copied from class doc)
69"""
70pass
71
72def__iter__(self): # real signature unknown; restored from __doc__
73""" x.__iter__() <==> iter(x) """
74pass
75
76def__len__(self): # real signature unknown; restored from __doc__
77""" x.__len__() <==> len(x) """
78pass
79
80def__le__(self, y): # real signature unknown; restored from __doc__
81""" x.__le__(y) <==> x<=y """
82pass
83
84def__lt__(self, y): # real signature unknown; restored from __doc__
85""" x.__lt__(y) <==> x<y """
86pass
87
88def__mul__(self, n): # real signature unknown; restored from __doc__
89""" x.__mul__(n) <==> x*n """
90pass
91
92    @staticmethod # known case of __new__
93def__new__(S, *more): # real signature unknown; restored from __doc__
94""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
95pass
96
97def__ne__(self, y): # real signature unknown; restored from __doc__
98""" x.__ne__(y) <==> x!=y """
99pass
100
101def__repr__(self): # real signature unknown; restored from __doc__ 102""" x.__repr__() <==> repr(x) """
103pass
104
105def__rmul__(self, n): # real signature unknown; restored from __doc__ 106""" x.__rmul__(n) <==> n*x """
107pass
108
109def__sizeof__(self): # real signature unknown; restored from __doc__ 110""" T.__sizeof__() -- size of T in memory, in bytes """
111pass 112
113 tuple tuple