简体   繁体   English

python编程中的线性搜索

[英]linear search in python programming

I have wriiten a code for linear search in python language. 我写了一个使用python语言进行线性搜索的代码。 The code is working fine for single digit numbers but its not working for double digit numbers or for numbers more than that. 该代码适用于一位数字,但不适用于两位数或更多数字。 Here is my code. 这是我的代码。

def linear_search(x,sort_lst):
    i = 0
    c= 0
    for i in range(len(sort_lst)):
        if sort_lst[i] == x :
             c= c+1
    if (c > 0):
      print ("item found")
    else : 
      print ("not found")
sort_lst= input("enter an array of numbers:")
item= input("enter the number to searched :")
linear_search(item,sort_lst)

any suggestions ? 有什么建议么 ?

Replace 更换

sort_lst= input("enter an array of numbers:")

with: 与:

print 'enter an array of numbers:'
sort_lst= map(int, raw_input().strip().split(' '))

If all you want is a substring search, you can just use this 如果您想要的只是子字符串搜索,则可以使用它

print("item found" if x in sort_lst else "not found")

If you want to get more complicated, then you need to convert your input from a string to an actual list. 如果您想变得更复杂,则需要将输入从字符串转换为实际列表。

(assuming space separated values) (假设使用空格分隔值)

sort_lst= input("enter an array of numbers:").split()

Then, that's actually a list of strings, not integers, but as long as you compare strings to strings, then your logic should work 然后,这实际上是一个字符串列表,而不是整数,但是只要您将字符串与字符串进行比较,那么逻辑就应该起作用

Note: the print statement above will still work in both cases 注意:以上两种情况下的打印语句仍然适用

This may be a case of confusion between behavior in python 2.x and python 3.x, as the behavior of the input function has changed . 这可能是python 2.x和python 3.x的行为混淆的情况,因为输入函数的行为已更改 In python 2, input would produce a tuple (12, 34) if you entered 12, 34 . 在Python 2, input将产生的元组(12, 34)如果输入12, 34 However, in python 3, this same function call and input produces "12, 34" . 但是,在python 3中,此相同的函数调用和输入产生"12, 34" Based on the parenthesis in your print s and the problem you're having, it seems clear you're using python 3 :-) 根据您的print的括号和您遇到的问题,似乎很明显您正在使用python 3 :-)

Thus when you iterate using for i in range(len(sort_lst)): , and then looking up the element to match using sort_lst[i] , you're actually looking at each character in the string "12, 34" (so "1", then "2", then ",", then " ", etc.). 因此,当您for i in range(len(sort_lst)):迭代使用,然后使用sort_lst[i]查找要匹配的元素时,实际上是在查看字符串 "12, 34" sort_lst[i]中的每个字符 (因此, 1”,然后是“ 2”,然后是“,”,然后是“”,等等。

To get the behavior you're after, you first need to convert the string to an actual list of numbers (and also convert the input you're matching against to a string of numbers). 要获得您想要的行为,您首先需要将字符串转换为实际的数字列表(还将匹配的输入转换为数字字符串)。

Assuming you use commas to separate the numbers you enter, you can convert the list using: 假设您使用逗号分隔输入的数字,则可以使用以下方法转换列表:

sorted_int_list = []
for number_string in sort_list.split(","):
    sorted_int_list = int(number_string.strip())

If you are familiar with list comprehensions, this can be shortened to: 如果您熟悉列表推导,可以将其简化为:

sorted_int_list = [int(number_string.strip()) for number_string in sort_list.spit(",")]

You'll also need: 您还需要:

item = int(item.strip())

To convert the thing you're comparing against from string to int. 要将您要比较的对象从string转换为int。

And I'm assuming you're doing this to learn some programming and not just some python, but once you've applied the above conversions you can in fact check whether item is in sorted_int_list by simply doing: 我假设您正在这样做是为了学习一些编程知识,而不仅仅是一些python,但是一旦您应用了上述转换,您实际上可以通过简单地检查item是否在sorted_int_list

is_found = item in sorted_int_list
if is_found:
    print ("Found it")
else:
    print ("Didn't find it :-(")

Notes: 笔记:

"12, 34".split(",") produces ["12", " 34"] , as the split function on strings breaks the string up into a list of strings, breaking between elements using the string you pass into it (in this case, ","). "12, 34".split(",")产生["12", " 34"] ,因为字符串的split函数将字符串分成字符串列表,并使用传递给它的字符串在元素之间进行分割(在这种情况下, ”,”)。 See the docs 查看文件

" 12 ".strip() trims whitespace from the ends of a string " 12 ".strip()从字符串的末尾修剪空白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM