【问题标题】:Linear Search Python线性搜索 Python
【发布时间】:2017-09-04 07:05:39
【问题描述】:

我是 python 新手,我们被分配创建一个不使用“in”或索引的线性搜索程序。该程序编译但说我输入的每个数字都不在列表中。我也必须为二进制搜索做同样的事情,但我一次只做一件事情哈。任何帮助表示赞赏!

PS:如果不使用“索引”功能,如何显示它所在的索引?

def linearSearch(intList,target):
    found = False
    position = 0
    while position < len(intList) and not found:
        if intList[position] == target:
            found = True
        position = position + 1

    return found

linearList = [3,5,9,7,6,12,15,9,1]
numInput = input("What number are you looking for? ")
numFound = linearSearch(linearList, numInput)
if numFound:
    print("The number is in index: ")
else:
    print("The number is not in the list")

【问题讨论】:

  • 这里有一个提示来解决你的问题:输入是字符串类型,你正在将它与整数进行比较

标签: python function binary-search linear-search


【解决方案1】:
def linearSearch(array,k):
    
    flag = False
    position = 0
    while position < len(intList) and not flag:
       
        if array[position] == k:
            flag = True
        else:
            position += 1

    return position

array = [3,5,9,7,6,12,15,9,1]
numInput = int(input("What number are you looking for? "))
numFound = linearSearch(linearList,numInput)
if numFound:
    print("The number is in index: ")
else:
    print("The number is not in the list")

【讨论】:

    【解决方案2】:

    此方法使用列表推导,它也将考虑列表中的任何重复项。它分配一个索引/索引列表,其中键出现在列表中。阅读有关列表理解的更多信息here.

    l = [1, 2, 3, 4, 4, 6]
    el = 4
    search = [i for i in range(len(l)) if el==l[i]]
    print(search)
    

    输出:

    [3, 4]
    

    或者,

    def LinSearch(target, intList):
         search = [i for i in range(len(intList)) if target==intList[i]]
         return search
    

    【讨论】:

      【解决方案3】:
       n=int(input("Enter the number:"))
       list,n=[10,11,12,13,14,15],n
       if n in list:
           print("Element found at",list.index(n))
       else:
           print("Not Found")
      

      建议:在 python 中,索引从 0 开始。要从 1 开始,格式如下:list.index(n)+1

      【讨论】:

        【解决方案4】:
        def linear_search(list, key):
        
            for i, item in enumerate(list):
                if item == key:
                    return i
            return -1
        
        print(linear_search([4,5,2,7,1,8],7))
        
        #If key is in the list returns its position in the list, otherwise returns -1.
        

        【讨论】:

        • 在 SO 中不鼓励仅代码答案。解释增加了长期价值,有助于传递有别于服务的信息和学习),并保持高品质。未来的访问者应该能够学习并将其传授给他们自己的编码问题。请考虑编辑以突出解决 OP 问题的解决方案的功能,解释它为什么有用或为什么选择该方法,或提供 OP 遗漏的见解。特别是对于家庭作业问题,重点是学习。有时,为引入的函数包含指向源文档的链接是有益的
        【解决方案5】:
        def target(list,num):
        
            for x in list:
                if x == str(num):
                    print("match found:"+x)
                    break
            else:
                 print('no match found')
        
        
        list1 = ['6','4','7','9','0','2','3','1']
        inp = input('Enter number to search:')
        target(list1,inp)
        

        【讨论】:

          【解决方案6】:

          线性搜索:

           // funtion which rturns true if item found inside list.
              def linearSearch(list, value):
                      for i in range(len(list)):
                          if i == value:
                               return True
          

          // 调用上面的函数传递值列表和要搜索的项目

              list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
              item = 10
              print(linearSearch(list, item)) // item to search
          

          【讨论】:

            【解决方案7】:
            def linearSearch(intList,target):
                #print (target)
                found = False
                position = 0
                while position < len(intList):
                    #print(intList[position])
                    if intList[position] == target:
                        found = True
                        break
                    position = position + 1
            
                return found
            
            linearList = [3,5,9,7,6,12,15,9,1]
            numInput = int(input("What number are you looking for? "))
            numFound = linearSearch(linearList,numInput)
            if numFound:
                print("The number is in index: ")
            else:
                print("The number is not in the list")
            

            请注意类型转换...

            【讨论】:

              【解决方案8】:

              1) 开始position = -1

              2)return position

              3) 你想在if intList[position] == target: 之前position+=1 并且当你找到元素时你想break。那么你就不需要found

              linearSearch(linearList, numInput) &gt; 0时发现了一些东西

              然后,您的代码不起作用,因为列表包含整数,而 input 将始终返回一个字符串。你必须使用int(input(".."))

              【讨论】:

              • 感谢您的帮助!我在发布后立即将输入从字符串更正为整数。我从 while 循环中收到错误“while position
              • 您以某种方式将 intList 分配给了一个 int。请自行调试代码
              • 提示:您的参数顺序是 1) 列表 2) 要查找的编号
              猜你喜欢
              • 2017-10-08
              • 1970-01-01
              • 1970-01-01
              • 2016-01-19
              • 2023-03-19
              • 1970-01-01
              • 2013-07-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多