Python:为什么列表没有 find 方法?

发布于 2024-09-25 20:54:18 字数 101 浏览 2 评论 0原文

我试图写一个答案这个问题 并且很惊讶地发现列表没有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

近箐 2024-10-02 20:54:18

我不知道为什么,或者可能埋在某个 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 the in operator. You can always make use of these 2 to find your items. (Also, re module, etc)

爱*していゐ 2024-10-02 20:54:18

我认为没有单独的“查找”和“索引”方法的理由是它们不够不同。如果列表中存在所查找的项目,两者都会返回相同的内容(这对于两个字符串方法都是如此);如果所寻找的项目不在列表/字符串中,则它们会有所不同;但是,您可以轻松地从另一个中构建查找/索引之一。如果您来自其他语言,那么在您可以轻松测试的非错误条件下引发和捕获异常似乎是不礼貌的行为,但在 Python 中,通常认为先拍摄然后再提出问题更Pythonic,呃,使用异常处理而不是这样的测试(例如:最好“尝试”某件事并捕获异常或测试是否可能首先避免异常?)。

我认为从“index”和“in”中构建“find”并不是一个好主意,

if foo in my_list:
   foo_index = my_list.index(foo)
else:
    foo_index = -1 # or do whatever else you want

因为 in 和 index 都需要 O(n) 遍历列表。

更好地从“索引”和 try/catch 中构建“查找”,例如:

try:
    foo_index = my_list.index(foo)
catch ValueError:
    foo_index = -1 # or do whatever else you want

现在,为什么列表以这种方式构建(仅使用索引),而字符串以另一种方式构建(使用单独的索引和查找)。我不能说。

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

if foo in my_list:
   foo_index = my_list.index(foo)
else:
    foo_index = -1 # or do whatever else you want

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:

try:
    foo_index = my_list.index(foo)
catch ValueError:
    foo_index = -1 # or do whatever else you want

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.

下壹個目標 2024-10-02 20:54:18

列表的“查找”方法是index

我确实认为 string.findlist.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 and list.index to be unfortunate, both in name and behavior: string.find returns -1 when no match is found, where list.index raises ValueError. This could have been designed more consistently. The only irreconcilable difference between these operations is that string.find searches for a string of items, where list.index searches for exactly one item (which, alone, doesn't justify using different names).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文