Python loop iteration issue

Two versions return opposite answers, but always one is mistaken. I do not know where I was wrong. I tried a number of other options, but it seems to be the closest. EDIT: need to be in a loop

goals: to identify the item in the list, determine when the item is not in the list, determine when the list is [], return rows accordingly.

def search_for_string(a_list, search_term):
    i=0
    for search_term in a_list:
        i += 1
        if a_list[i] == search_term: 
            return 'string found!' 
        elif a_list[i] != search_term:
            return 'string not found2'
    if len(a_list) == 0:
        return 'string not found'

apple = search_for_string(['a', 'b', 'c'], 'd')
print(apple)


def search_for_string(a_list, search_term):
    i=0
    for search_term in a_list:
        if a_list[i] == search_term: 
            return 'string found!' 
        elif a_list[i] != search_term:
            return 'string not found2'
        i += 1
    if len(a_list) == 0:
        return 'string not found'

apple = search_for_string(['a', 'b', 'c'], 'd')
print(apple)

other tests:

apple = search_for_string(['a', 'b', 'c'], 'b')
apple = search_for_string([], 'b')
+4
source share
8 answers

Python makes your life ultra-light for this kind of thing:

def search_for_string(a_list, search_term):
    if search_term in a_list:
        return 'string found!'
    return 'string not found'
+9
source

There are several incorrect and non-python code:

def search_for_string2(a_list, search_term):
    i=0  # <----- Not Pythonic! If you want to get index we use enumerate(a_list)
    for search_term in a_list: # <--- search_term passed to function is lost and gets overwritten by elements in a_list.
        i += 1 # <--- Not Pythonic in this context
        if a_list[i] == search_term: #<--- a_list[index+1] == a_list[index]. True if consecutive elements are same else False!
            return 'string found!' #<--- No WRONG!, You didn't find the string, Consecutive elements are same!
        elif a_list[i] != search_term:
            return 'string not found2' #<-- Consecutive elements are not same!
    if len(a_list) == 0:
        return 'string not found'

In accordance with your goals, you can implement it like this:

def search_for_string(alist, search_term):
    if not alist:
        return "List is empty"
    if search_term in alist:
        return "First occurence of string Found at index position: " + str(alist.index(search_term))
    else:
        return "String not found"


print(search_for_string(['a', 'b', 'c'], 'd'))
print(search_for_string(['a', 'b', 'c'], 'b'))
print(search_for_string([], 'b'))

Conclusion:

String not found
First occurence of string Found at index position: 1
List is empty
+3

, != , , , , , 1. , :

def search_for_string(haystack, needle):
    if not haystack: # check for empty list
        return 'List was empty!'
    for x in haystack:
        if needle == x:
            return 'String found!'
    return 'String not found!'

, , , . , , , .


, :

  • , (1) (2) String, / :

    def search_for_string(a_list, search_term):
        i=0
        for search_term in a_list:
            i += 1
            if a_list[i] == search_term: # whoops this comparison checks for succeeding elements!
                return 'string found!' 
            elif a_list[i] != search_term: # whoops this part returns  even before all succeeding elements are checked.
                return 'string not found2'
        if len(a_list) == 0:
            return 'string not found'
    
    apple = search_for_string(['a', 'b', 'c'], 'd')
    # In the list ['a', 'b', 'c']
    # element [0] = 'a'
    # element [1] = 'b'
    # element [2] = 'c'
    print(apple)
    

, :

# search_term == 'd'
# a_list = [ 'a', 'b', 'c' ]
i = 0 # at this point i == 0
for search_term in a_list:  
    # Oh no!  we lost the search term that we passed into the 
    # function because we are using it as the loop iterator
    # search_term == 'a'
    i += 1 # i == 1
    if a_list[i] == search_term: 
        # checks to see if 'b' == 'a'
        return 'string found!'
    elif a_list[i] != search_term:
        # checks to see if 'b' != 'a'
        return 'string not found!' 
        # and we return after one iteration of the loop.

(1) (2), , .

+2

search_for_string - .

, search_term. , .

​​ .

def search_for_string(a_list, search_item):
  if(len(a_list) == 0):
       return 'List is empty'
  else:
    for search_term in a_list:
        if search_term == search_item: 
            return 'string found!' 
    return 'string not found'
+1

. , - . :

  • search_term , , for.
  • a_list , i . . , .
  • , a_list . . , if . for , a_list .

, :

>>> def search_for_string(lst, key):
    # only iterate by value.
        for string in lst:
            # we only need to test once
            # if `key` is equal to the
            # current string we are on.
            if string == key:
                return 'string found'
        # no need to test if the list
        # is empty. The for loop will
        # never be run if it is, and
        # this return statement will
        # execute.
        return 'string not found'

>>> search_for_string(['a', 'b', 'c'], 'd')
'string not found'
>>> search_for_string(['a', 'b', 'c'], 'b')
'string found'
>>> search_for_string([], 'b')
'string not found'
>>> 
+1

, . search_term, for x in y x y. , for x in [1, 2, 3], x = 1 .. , , 'a' == 'b', , '==' a ', , , !

-

x in list

True False, x ! ( "list", , ).

, Pythonic

def search_for_string(a_list, search_term):
    if search_term in a_list:
        return 'string found!'
    elif not a_list:  # realistically you'd put this before here but I'm trying to mirror your code--why might you put this earlier? Because it less costly than searching a list.
        return 'empty list!'
    else:
        return 'string not found!'

, bool([]) False, , .

-, , , .

def search_for_string(a_list, search_term):
    for index, item in enumerate(a_list):
        if a_list[index] == search_term:
            return 'string found!'
            # what do you think the value of 'item' is here? it equal to a_list[index]!
        elif len(a_list) == 0:  # again, you'd put this earlier--why make your computer do the work? it doesn't have to. Also, you could just do elif not a_list
            return 'string not found'
        else: 
            continue
    return 'string not found2'
+1

, , , , @Stephen Rauch, Pythonic- .

, , , .

return , .

, , for, , a_list, "", , " ", , .

, .

+1

, if. i, . , python. , .

def search_for_string(a_list, search_term):

    #if a_list is empty, return False
    if len(a_list) == 0:
          return False
    #if search_term has an element in a_list return the string
    if search_term in a_list:
          return "string found"

    return "string not found"
+1

Source: https://habr.com/ru/post/1666120/


All Articles