线性搜索Python

时间:2017-04-09 03:48:46

标签: python function binary-search linear-search

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

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")

8 个答案:

答案 0 :(得分:1)

1)开始position = -1

2)return position

3)您希望在position+=1之前if intList[position] == target:,并且在找到元素时想要break。然后,您不需要found

linearSearch(linearList, numInput) > 0

时找到了一些东西

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

答案 1 :(得分:1)

此方法使用列表推导,并且还将考虑列表中的所有重复项。它分配一个索引列表,其中索引键出现在列表中。了解有关列表理解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

答案 2 :(得分:0)

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")

请注意类型转换...

答案 3 :(得分:0)

线性搜索:

 // 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

答案 4 :(得分:0)

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)

答案 5 :(得分:0)

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.

答案 6 :(得分:0)

rules = [Rule(LinkExtractor(deny=[r'blank\\.html',
                              r'isin\\=',
                              r'_frame\\.htm',
                              r'actionbar',
                              r'actionframe',
                              r'popup_times_and_sales',
                              r'ID_NOTATION',
                              r'suchergebnis',
                              r'DATE\\=',
                              r'NOTATION\\=']),

建议:在python中,索引从0开始。要从1开始,请格式化以下代码:Module parse failed: Unexpected token (1:9) File was processed with these loaders: * ../../node_modules/@ngtools/webpack/src/index.js You may need an additional loader to handle the result of these loaders. > export * as areacharts from './area-charts';

答案 7 :(得分:0)

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")