user2928929
user2928929

Reputation: 205

Search function python

Hi Im trying to create a search function in python, that goes through a list and searches for an element in it.

so far ive got

def search_func(list, x)

      if list < 0:
            return("failure")
      else:
            x = list[0]

      while x > list:
            x = list [0] + 1    <----  how would you tell python to go to the next element in the list ? 
      if (x = TargetValue):
          return "success"
      else 
          return "failure" 

Upvotes: 1

Views: 17851

Answers (5)

Wool
Wool

Reputation: 17

def search(query, result_set):
    if isinstance(query, str):
        query = query.split()
    assert isinstance(query, list)
    results = []
    for i in result_set:
        if all(quer.casefold() in str(i).casefold() for quer in query):
            results.append(i)
    return results

Works best.

Upvotes: 0

rahul
rahul

Reputation: 21

You are making your problem more complex, while solving any problem just think before starting to code. You are using while loops and so on which may sometimes becomes an infinite loop. You should use a for loop to solve it. This is better than while loop. So just check which condition helps you. That's it you are almost done.

def search_func(lst,x):
    for e in lst: #here e defines  elements in the given list
        if e==x:  #if condition checks whether element is equal to x
            return True
        else:
            return False

Upvotes: 0

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236150

Well, you current code isn't very Pythonic. And there are several mistakes! you have to use indexes to acces an element in a list, correcting your code it looks like this:

def search_func(lst, x):
    if len(lst) <= 0:   # this is how you test if the list is empty
        return "failure"
    i = 0               # we'll use this as index to traverse the list
    while i < len(lst): # this is how you test to see if the index is valid
        if lst[i] == x: # this is how you check the current element
            return "success"
        i += 1          # this is how you advance to the next element
    else:               # this executes only if the loop didn't find the element
        return "failure" 

... But notice that in Python you rarely use while to traverse a list, a much more natural and simpler approach is to use for, which automatically binds a variable to each element, without having to use indexes:

def search_func(lst, x):
    if not lst:    # shorter way to test if the list is empty
        return "failure"
    for e in lst:  # look how easy is to traverse the list!
        if e == x: # we no longer care about indexes
            return "success"
    else:
        return "failure" 

But we can be even more Pythonic! the functionality you want to implement is so common that's already built into lists. Just use in to test if an element is inside a list:

def search_func(lst, x):
    if lst and x in lst: # test for emptiness and for membership
        return "success"
    else:
        return "failure"

Upvotes: 4

user2555451
user2555451

Reputation:

Are you saying you want to see if an element is in a list? If so, there is no need for a function like that. Just use in:

>>> lst = [1, 2, 3]
>>> 1 in lst
True
>>> 4 in lst
False
>>>

This method is a lot more efficient.


If you have to do it without in, I suppose this will work:

def search_func(lst, x):
    return "success" if lst.count(x) else "failure" 

Upvotes: 3

you dont need to write a function for searching, just use

x in llist

Update:

def search_func(llist,x):
      for i in llist:
          if i==x:
             return True
      return False

Upvotes: 1

Related Questions