“如果不在”在Python中获取索引

这个问题在这里已经有了答案:

  • 在Python中找到一个包含它的列表的索引23个答案

  • >>> 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

    上一篇: "if not in" in python to get the index

    下一篇: Finding the position of words in a string