Humty
Humty

Reputation: 1361

Comparison of two list in python

I want to compare two lists, if both have the same word with its match number. The match number is important here. I did it in this way;

    List1= ['john', 'doe','sima']
    List2=[]
    test = "John is with Doe but alina is alone today."
    List2 = test.lower().split()
    n=0
    counter=0
    while n < len(List1):
        for i in range(len(List2)):
            if List1[n] == List2[i]:
                print("Matched : "+str(counter) + List1[n])
                n=n+1
                counter=counter+1
            else:
                print("No match :"+ List1[n])
#             break
#         break

The program is working fine, if both lists have the matched words. But for unmatched word sima, the loop is running infinite times. If break the for loop in else and then break the while loop just after it as comment is telling in the code, the program run for first match only. Thanks in advance.

Edit 1

 while n < len(List1):
        for i in range(len(List2)):
#         print("Matching :"+ List1[n]+ " : "+ List2[i])
            if List1[n] == List2[i]:
                print("Matched : "+str(counter) + List1[n])

                counter=counter+1
            else:
                print("No match :"+ List1[n])
            n=n+1

Giving IndexError: list index out of range error

Upvotes: 1

Views: 75

Answers (3)

Harry
Harry

Reputation: 318

List1= ['john', 'doe','sima', 'alina' ]
List2=[]
test = "John is with Doe but alina is alone today."
List2 = test.lower().split()
counter = 0
for word in List1:
    try:
        index_number = List2.index(word)
        counter += 1
        print("Matched : " + str(counter) + " " +  word + " at " + str(index_number))
    except:
        print("No Match Found")

Although solution to your problem is already answered by others still its not elegant. As in the question you mentioned that the match number is important, so I am giving you my way of solution for the problem. Please look.

Upvotes: 1

Athena
Athena

Reputation: 543

The problem occurs due to a small problem. You are increasing n in if body which means increasing the variable if the condition is met. In your case, when it reaches to sima, the condition is not met and so n will not be increased. So you need to increment n after for loop.

Upvotes: 0

Dinesh.hmn
Dinesh.hmn

Reputation: 711

From your code, this will work. Although not the most elegant way of writing it, here is your code

List1= ['john', 'doe','sima']
List2=[]
test = "John is with Doe but alina is alone today."
List2 = test.lower().split()
n=0
counter=0
while n < len(List1):
    for i in range(len(List2)-1):
        if List1[n] == List2[i]:
            print("Matched : "+str(counter) + List1[n])
            counter=counter+1
        else:
            print("No match :"+ List1[n])
    n=n+1

And this is your result

Matched : 0john
No match :john
No match :john
No match :john
No match :john
No match :john
No match :john
No match :john
No match :doe
No match :doe
No match :doe
Matched : 1doe
No match :doe
No match :doe
No match :doe
No match :doe
No match :sima
No match :sima
No match :sima
No match :sima
No match :sima
No match :sima
No match :sima
No match :sima

Upvotes: 2

Related Questions