1

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

7
  • use set and & . Commented Feb 2, 2017 at 5:15
  • 1
    move the n = n+1 line to the end of the loop outside the "if" condition. If the "if" condition fails, then n never increases and the loop goes on Commented Feb 2, 2017 at 5:15
  • @Dinesh.hmn See the edit1 in question Commented Feb 2, 2017 at 5:18
  • @YOU I need counter for each match. How can I get counter using set and & ? Commented Feb 2, 2017 at 5:19
  • A list works from 0 to n-1. If you type list[n] it will give you an error saying list index out of range Commented Feb 2, 2017 at 5:22

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1
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.

2 Comments

@ Harry, thanks for writing it in an elegant way. from his code what I see is someone who just started so I did not want to use advanced code to make it hard for him, upvoted.
Yeah that's why I upvoted your answer. But I still thought to write it for his future references :)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.