Home > Net >  Check to see if two lists have the same value at the same index, if so return the index. If not retu
Check to see if two lists have the same value at the same index, if so return the index. If not retu

Time:11-27

So basically I am trying to compare two lists to see if they hold the same value at the same index at any point. If they do I return the index, if they do not, I return -1. `

Dlist = [17,13,10,6,2]
Ilist = [5,9,10,15,18]
def seqsearch(DS,IS):
    
    for i in range(len(DS)-1):
        found = False
        if DS[i] == IS[i]:
            answer = i
            found = True
            if found == True:
                print(f"Yes! Found at index =", answer)
            else:
                return print("No!\n-1")


print(seqsearch(Dlist,Ilist))

`

When I had first done this as a test I was having no issues however adding in the text has made it more difficult and my main issue is with the if else statement. I seem to be able to only get one message to work, either yes or no, not both based on the case.

CodePudding user response:

You could loop over both lists like so:

Dlist = [17, 13, 10, 6, 2]
Ilist = [5, 9, 10, 15, 18]


def seqsearch(DS, IS):

    for index_1, element_1 in enumerate(DS):
        for index_2, element_2 in enumerate(IS):

            if (element_1 == element_2) and (index_1 == index_2):
                print(f"Yes! Found at index ={index_1}")
                return index_1
    print("No!")
    return -1


print(seqsearch(Dlist, Ilist))

However, there are more improvements you can make. zip() indeed is a better option, but slightly more complicated to understand.

Also, please note that return is not the same as print. You were not returning -1; you were printing it.

CodePudding user response:

I think a cleaner answer uses the built-in enumerate and zip functions:

Dlist = [17,13,10,6,2]
Ilist = [5,9,10,15,18]

def seqsearch(DS,IS):
    for idx, (d, s) in enumerate(zip(DS, IS)):
        if d == s:
            return f"Yes! Found at index = {idx}"

    return "No!\n-1"


print(seqsearch(Dlist,Ilist))

It's unclear whether you want to return just the first index, or all the indices with matching elements. In either case, it is probably better if you just return the desired value and then add any formatting and print statements outside of the function's scope.

CodePudding user response:

i try use your code for better undrestand (but based of your use we have many option)

Dlist = [17, 13, 10, 6, 2]
Ilist = [5, 9, 10, 15, 18]


def seqsearch_with_print(DS, IS):
    """this function only print not return!!!
    if you use return your function ended!"""
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            print(f"Yes! Found at index {i}")
        else:
            print(f"No not equal in {i} index")


def seqsearch_with_list(DS, IS):
    """this function save history of (equal index or not: as 1, -1)"""
    temp_list = []
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            temp_list.append(1)
        else:
            temp_list.append(-1)
    return temp_list


def seqsearch_with_list_dict(DS, IS):
    """this function save history of (equal index or not: as 1, -1)
    as key, value (key== index, value==list item)"""
    temp_list = []
    for i in range(len(DS) - 1):
        if DS[i] == IS[i]:
            temp_list.append({i: 1})
        else:
            temp_list.append({i: -1})
    return temp_list


# no need print(in function we used print)
seqsearch_with_print(Dlist, Ilist)
"""
return of function: 
    No not equal in 0 index
    No not equal in 1 index
    Yes! Found at index 2
    No not equal in 3 index"""

# save history of search index in list
print(seqsearch_with_list(Dlist, Ilist))
"""
return of function with print:
    [-1, -1, 1, -1]
"""
# save history as dict key value
print(seqsearch_with_list_dict(Dlist, Ilist))
"""
return of function with print
    [{0: -1}, {1: -1}, {2: 1}, {3: -1}]
"""

  • Related