搜索阵列报告“未找到".即使找到了 [英] Searching array reports "not found" even though it's found

查看:67
本文介绍了搜索阵列报告“未找到".即使找到了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对常见错误的解答,这是我在使用各种语言的新程序员的许多问题中看到的逻辑错误.

This is a generic question and answer for a logical error I've seen in many questions from new programmers in a variety of languages.

问题是在数组中搜索与某些输入条件匹配的元素.伪代码中的算法看起来像这样:

The problem is searching an array for an element that matches some input criteria. The algorithm, in pseudo-code, looks something like this:

for each element of Array:
    if element matches criteria:
        do something with element
        maybe break out of loop (if only interested in first match)
    else:
        print "Not found"

即使成功找到匹配的元素,此代码也会报告未找到".

This code reports "Not found" even if it successfully finds a matching element.

推荐答案

问题是,当您通过数组线性搜索某些内容时,直到到达数组末尾时才知道找不到该内容.问题中的代码会为每个不匹配的元素报告未找到",即使可能还有其他匹配的元素.

The problem is that when you're searching for something linearly through an array, you can't know that it's not found until you reach the end of the array. The code in the question reports "Not found" for every non-matching element, even though there may be other matching elements.

简单的修改是使用一个变量来跟踪您是否发现了某些东西,然后在循环结束时检查该变量.

The simple modification is to use a variable that tracks whether you found something, and then check this variable at the end of the loop.

found = false
for each element of Array:
    if element matches criteria:
        do something with element
        found = true
        maybe break out of loop (if only interested in first match)

if not found:
    print "Not found"

Python在其for循环中有一个else:块.仅当循环运行完成时才执行代码,而不是由于使用break而结束.这样可以避免使用found变量(尽管它对于以后的处理可能仍然有用):

Python has an else: block in its for loops. This executes code only if the loop runs to completion, rather than ending due to use of break. This allows you to avoid the found variable (although it might still be useful for later processing):

for element in someIterable:
    if matchesCriteria(element):
        print("Found")
        break
else:
    print("Not found")

某些语言具有内置机制,可用来代替编写自己的循环.

Some languages have built-in mechanisms that can be used instead of writing your own loop.

  • 某些语言具有anysome函数,该函数带有回调函数,并返回一个布尔值,指示该数组是否对数组的任何元素都成功.
  • 如果该语言具有数组过滤功能,则可以使用检查条件的函数过滤输入数组,然后检查结果是否为空数组.
  • 如果您想精确匹配元素,则大多数语言都提供findindex函数,这些函数将搜索匹配的元素.
  • Some languages have an any or some function that takes a callback function, and returns a boolean indicating whether it succeeds for any elements of the array.
  • If the language has an array filtering function, you can filter the input array with a function that checks the criteria, and then check whether the result is an empty array.
  • If you're trying to match an element exactly, most languages provide a find or index function that will search for a matching element.

如果您要经常搜索,最好将数组转换为可以更有效地搜索的数据结构.大多数语言都提供set和/或hash table数据结构(后者根据语言使用了很多名称,例如,关联数组,地图,字典),并且通常在O(1)时间内可以搜索到它们,同时扫描数组是O(n).

If you'll be searching frequently, it may be better to convert the array to a data structure that can be searched more efficiently. Most languages provide set and/or hash table data structures (the latter goes under many names depending on the language, e.g. associative array, map, dictionary), and these are typically searchable in O(1) time, while scanning an array is O(n).

这篇关于搜索阵列报告“未找到".即使找到了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆