use*_*763 1 list find python-3.x
如何检查一个元素是否在列表中排在另一个元素之前?
例如:
如何检查列表中 5 是否在 12 之前:
li = [1,2,3,7,4,5,10,8,9,12,11]
Run Code Online (Sandbox Code Playgroud)
是否有内置的 Python 函数可以让我这样做?
小智 5
给你:
>>> li = [1,2,3,7,4,5,10,8,9,12,11]
>>> li.index(5) > li.index(12) # 5 comes after 12
False
>>> li.index(5) < li.index(12) # 5 comes before 12
True
>>>
>>> help(list.index)
Help on method_descriptor:
index(...)
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
>>>
Run Code Online (Sandbox Code Playgroud)