简体   繁体   中英

Match elements between two lists but index for index

Lets say I have these 2 listst:

lst1 = ['A1','B1','C1']

lst2 = ['A1','B2']

For every element in lst1 compare to the corresponding element in lst2 by index (1-1 not elem 1 to all elems in lst2). If a match is found print an output like this: 'match found between A1 and A1 at index 0 in both lists'

If the elements don't match an output like this should be given: 'Elements at index 1 do not match"

If element at index 2 in lst1 has no coresponding index in lst2 print out: 'For element at index 3, 'C1', no corresponding match found in lst3.

What i've tried:

 for i in range(len(lst1)):

            if lst1[i] == lst2[i]:

                return 'Match between...'
            else:
                return 'Not matched...'

It failes when the lists do not match in lenght, besides that i'm sure there's a smarter way to do this.

Try this:

lst1 = ['A1','B1','C1']
lst2 = ['A1','B2']

max_length = max(len(lst1), len(lst2))
for i in range(max_length):
    try:
        if lst1[i] == lst2[i]:
            print(f'match found between {lst1[i]} and {lst2[i]} at index {i} in both lists')
        else:
            print(f"Elements at index {i} do not match")

    except:
        if len(lst1) >= i:
            print(f"For element at index {i}, {lst1[i]}, no corresponding match found in lst2.")
        else:
            print(f"For element at index {i}, no corresponding match found in lst2. {lst2[i]}")

Which will produce this output:

match found between A1 and A1 at index 0 in both lists
Elements at index 1 do not match
For element at index 2, C1, no corresponding match found in lst2.

In this situation, I would urge you to use itertools.zip_longest() .

from itertools import zip_longest

lst1 = ["A1", "B1", "C1"]
lst2 = ["A1", "B2"]
ziplist = list(zip_longest(lst1, lst2))

for i in range(len(ziplist)):
    print(ziplist[i][0], ziplist[i][1])
    if (ziplist[i][0] is None) and (ziplist[i][1] is not None):
        print(
            "For element at index {}, {} , no corresponding match found in lst1".format(
                i, ziplist[i][1]
            )
        )
    elif (ziplist[i][0] is not None) and (ziplist[i][1] is None):
        print(
            "For element at index {}, {} , no corresponding match found in lst1".format(
                i, ziplist[i][0]
            )
        )
    elif ziplist[i][0] == ziplist[i][1]:
        print(
            "match found between {} and {} at index {} in both lists".format(
                ziplist[i][0], ziplist[i][1], i
            )
        )
    elif ziplist[i][0] != ziplist[i][1]:
        print("Elements at index {} do not match".format(i))

I am using zip_longest(lst1, lst2, fillvalue=None) fillvalue as None over here but you can use whatever fillvalue you want according to your data in the list so you can easily identify if something doesn't exist. You can read more about itertools.zip_longest() here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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