"if not in" in python to get the index

This question already has an answer here:

  • Finding the index of an item given a list containing it in Python 23 answers

  • >>> 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/28126.html

    上一篇: 如果a不是列表中的元素如何打印?

    下一篇: “如果不在”在Python中获取索引