“如果不在”在Python中获取索引
这个问题在这里已经有了答案:
>>> a=[2,3,4]
>>> a.index(3)
1
编辑
>>> a=[2,3,4]
>>> def check(n):
... try:
... print a.index(n)
... except ValueError:
... print "Element not found in list a"
...
>>> check(5)
Element not found in list a
>>> check(3)
1
链接地址: http://www.djcxy.com/p/28125.html