index()

一般用处是在序列中检索参数并返回第一次出现的索引,没找到就会报错,比如:

>>> t=tuple('Allen')
>>> t
('A', 'l', 'l', 'e', 'n')
>>> t.index('a')
Traceback (most recent call last):
 File "<pyshell#2>", line 1, in <module>
  t.index('a')
ValueError: tuple.index(x): x not in tuple
>>> t.index('e')
3
>>> t.index('l')
1

但参数可能会出现很多次,要如何做呢?

相关

python中index的用法是什么