Python 元组

元组是不可变类型,

主要方法有:count, index

count: 统计某元素出现的次数

In [10]: t = (1,2,'3')

In [11]: t.count(1)
Out[11]: 1

In [12]: t2 = (1,2,1,1,'3')

In [13]: t2.count(1)
Out[13]: 3

index: 返回第一个匹配的元素的索引,如果不存在则报错

In [14]: t.index(1)
Out[14]: 0

In [15]: t.index(2)
Out[15]: 1

In [16]: t.index(4)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-e6d87ee05869> in <module>
----> 1 t.index(4)

ValueError: tuple.index(x): x not in tuple

 

上一篇:Python 列表

下一篇:Python 类中super的作用