问题
In Python, find
and index
are very similar methods, used to look up values in a sequence type. find
is used for strings, while index
is for lists and tuples. They both return the lowest index (the index furthest to the left) that the supplied argument is found.
For example, both of the following would return 1
:
"abc".find("b")
[1,2,3].index(2)
However, one thing I'm somewhat confused about is that, even though the two methods are very similar, and fill nearly the same role, just for different data types, they have very different reactions to attempting to find something not in the sequence.
"abc".find("d")
Returns -1
, to signify 'not found', while
[1,2,3].index(4)
raises an exception.
Basically, why do they have different behaviors? Is there a particular reason, or is it just a weird inconsistency for no particular reason?
Now, I'm not asking how to deal with this – obviously, a try
/except
block, or a conditional in
statement, would work. I'm simply asking what the rationale was for making the behavior in just that particular case different. To me, it would make more sense to have a particular behavior to say not found, for consistency's sake.
Also, I'm not asking for opinions on whether the reason is a good reason or not – I'm simply curious about what the reason is.
Edit: Some have pointed out that strings also have an index
method, which works like the index
method for lists, which I'll admit I didn't know, but that just makes me wonder why, if strings have both, lists only have index
.
回答1:
This has always been annoying ;-) Contrary to one answer, there's nothing special about -1 with respect to strings; e.g.,
>>> "abc"[-1]
'c'
>>> [2, 3, 42][-1]
42
The problem with find()
in practice is that -1 is in fact not special as an index. So code using find()
is prone to surprises when the thing being searched for is not found - it was noted even before Python 1.0.0 was released that such code often went on to do a wrong thing.
No such surprises occur when index()
is used instead - an exception can't be ignored silently. But setting up try/except
for such a simple operation is not only annoying, it adds major overhead (extra time) for what "should be" a fast operation. Because of that, string.find()
was added in Python 0.9.9 (before then, only string.index()
could be used).
So we have both, and that persists even into Python 3. Pick your poison :-)
来源:https://stackoverflow.com/questions/20410385/python-why-do-the-find-and-index-methods-work-differently