Python:为什么列表没有 find 方法?
我试图写一个答案这个问题 并且很惊讶地发现列表没有find方法,列表只有index方法(字符串有find和index)。
谁能告诉我这背后的理由吗? 为什么弦乐两者都有呢?
I was trying to write an answer to this question and was quite surprised to find out that there is no find method for lists, lists have only the index method (strings have find and index).
Can anyone tell me the rationale behind that?
Why strings have both?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道为什么,或者可能埋在某个 PEP 中,但我确实知道 2 个非常基本的列表“查找”方法,它们是 array.index() 和 in 运算符。您始终可以利用这两个来查找您的物品。 (另外,重新模块等)
I don't know why or maybe is buried in some PEP somewhere, but i do know 2 very basic "find" method for lists, and they are
array.index()
and thein
operator. You can always make use of these 2 to find your items. (Also, re module, etc)我认为没有单独的“查找”和“索引”方法的理由是它们不够不同。如果列表中存在所查找的项目,两者都会返回相同的内容(这对于两个字符串方法都是如此);如果所寻找的项目不在列表/字符串中,则它们会有所不同;但是,您可以轻松地从另一个中构建查找/索引之一。如果您来自其他语言,那么在您可以轻松测试的非错误条件下引发和捕获异常似乎是不礼貌的行为,但在 Python 中,通常认为先拍摄然后再提出问题更Pythonic,呃,使用异常处理而不是这样的测试(例如:最好“尝试”某件事并捕获异常或测试是否可能首先避免异常?)。
我认为从“index”和“in”中构建“find”并不是一个好主意,
因为 in 和 index 都需要 O(n) 遍历列表。
更好地从“索引”和 try/catch 中构建“查找”,例如:
现在,为什么列表以这种方式构建(仅使用索引),而字符串以另一种方式构建(使用单独的索引和查找)。我不能说。
I think the rationale for not having separate 'find' and 'index' methods is they're not different enough. Both would return the same thing in the case the sought item exists in the list (this is true of the two string methods); they differ in case the sought item is not in the list/string; however you can trivially build either one of find/index from the other. If you're coming from other languages, it may seem bad manners to raise and catch exceptions for a non-error condition that you could easily test for, but in Python, it's often considered more pythonic to shoot first and ask questions later, er, to use exception handling instead of tests like this (example: Better to 'try' something and catch the exception or test if its possible first to avoid an exception?).
I don't think it's a good idea to build 'find' out of 'index' and 'in', like
because both in and index will require an O(n) pass over the list.
Better to build 'find' out of 'index' and try/catch, like:
Now, as to why list was built this way (with only index), and string was built the other way (with separate index and find)... I can't say.
列表的“查找”方法是
index
。我确实认为
string.find
和list.index
之间的不一致在名称和行为上都是不幸的:string.find
返回 -1当未找到匹配项时,list.index
会引发 ValueError。这本来可以设计得更加一致。这些操作之间唯一不可调和的区别是string.find
搜索一串项目,而list.index
只搜索一个项目(仅此一项并不证明使用不同的名称是合理的)。The "find" method for lists is
index
.I do consider the inconsistency between
string.find
andlist.index
to be unfortunate, both in name and behavior:string.find
returns -1 when no match is found, wherelist.index
raises ValueError. This could have been designed more consistently. The only irreconcilable difference between these operations is thatstring.find
searches for a string of items, wherelist.index
searches for exactly one item (which, alone, doesn't justify using different names).