【问题标题】:Linear Search - Python线性搜索 - Python
【发布时间】:2017-10-08 22:44:59
【问题描述】:

我是新手,刚开始学习 Python。我在课程中的第一个作业要求我对国家列表执行线性搜索(我知道线性搜索很糟糕,但这只是为了练习:))我找不到用于简单线性搜索的代码不涉及整数。

我假设第一步是创建一个数组,它是:

listCountries = ['France', 'Spain', 'United Kingdom', 'Italy', 'Portugal', 'Ireland', 'Poland', 'Norway']

我需要搜索“西班牙” - 我将使用什么代码?

提前致谢

【问题讨论】:

  • 为什么对整数的线性搜索与对字符串、国家等的线性搜索有很大不同?
  • 就像我说的我对编程非常陌生,我假设我们使用不同的代码,具体取决于数组是由整数还是字符/措辞组成。
  • 好吧,Python 是一种动态语言,这意味着您可以将任何内容放入列表中。因此,在许多(并非所有情况)中,适用于整数列表的算法也适用于马、汽车、...的列表
  • 啊哈,好的,谢谢。我不知道(我基本上是在自学这门 CS 课程)。我找到了在列表中查找整数的代码,在代码中,我看到它说“int”——我认为它只适用于整数。我找不到合适的代码来处理字符、单词等。Anywho,这让事情变得更简单了。

标签: python


【解决方案1】:

如果您想知道“西班牙”是否在列表中,您可以这样做:

'Spain' in listCountries ## returns true if the string 'Spain' is an element of listCountries

有类似的内置函数来查找它的索引等。

如果你想手动(练习)你可以这样做:

def inList (l, elem)
   for e in l:
     if e == elem:
       return True
   return False

这将遍历所有列表元素,如果找到您要查找的元素,它将返回True,如果未找到您要查找的元素,则返回False

如果您还关心元素的索引,您可以这样做:

def whereInList (l,elem): ## return the index of desired element, if not in list return None
  for i,e in enumerate(l):
    if e == elem:
      return i
  return None  

【讨论】:

    【解决方案2】:

    假设您知道线性搜索算法,我认为您在比较字符串而不是整数时会遇到问题。 (如果没有,请使用this

    如果您想按字典顺序比较字符串,Python 中的布尔运算符可以为您完成这项工作。从这个意义上说,您的整数和字符串代码不会有所不同。希望对你编写有帮助,因为我不想直接给你代码。

    您可以阅读here了解更多详情。

    【讨论】:

    • 我查看了第一个链接,它真的很有帮助,非常感谢。
    【解决方案3】:
    countries = ["France", "Spain", "United Kingdom", "Italy", "Portugal", "Ireland", "Poland", "Norway"]
    
    countrie_to_search = 
    
    for index, item in enumerate(countries, 0):
        print("index: {} country: {}".format(index, item))
        if item = countrie_to_search:
            # we have a match, do what you want
    

    【讨论】:

      【解决方案4】:

      非pythonic但易于遵循的方法。

      listCountries = ['France', 'Spain', 'United Kingdom', 'Italy', 'Portugal', 'Ireland', 'Poland', 'Norway']
      
      i=0
      l = len(listCountries)
      while i<l:
        if listCountries[i] == "Spain":
            print "Spain found at index", i
            break;
        else:
            i=i+1
      

      还有这个

      for j in range(len(listCountries)):
          if listCountries[j] == "Spain":
              print "Spain found at index", j
          else:
              continue
      

      当然,您可以将 sn-ps 包装在这样的函数中,

      def look_for(c, data):
          for j in range(len(data)):
              if data[j] == c:
                  return c+" found at index "+str(j)
              else:
                continue
          return c+" not found"
      
      #print look_for("Spain", listCountries) => will return Spain found at index 1
      #print look_for("USSR", listCountries) => USSR not found
      #print look_for("Finland", listCountries) => Finland not found
      

      下面的代码将为您做所有事情 -))

      print listCountries.index("Spain")  
      

      【讨论】:

      • 太棒了——感谢一百万。我现在已经掌握了窍门!
      猜你喜欢
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      • 2023-03-19
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多