Finding the index of an item in a list

后端 未结 30 3279
你的背包
你的背包 2020-11-21 05:28

Given a list [\"foo\", \"bar\", \"baz\"] and an item in the list \"bar\", how do I get its index (1) in Python?

30条回答
  •  广开言路
    2020-11-21 05:59

    One thing that is really helpful in learning Python is to use the interactive help function:

    >>> help(["foo", "bar", "baz"])
    Help on list object:
    
    class list(object)
     ...
    
     |
     |  index(...)
     |      L.index(value, [start, [stop]]) -> integer -- return first index of value
     |
    

    which will often lead you to the method you are looking for.

提交回复
热议问题