1

I would like to Compare two lists and show the amount of matches until no match is found.

for example if i have

list1 = [1,2,3,4,5,6]
list2 = [1,2,3,4,5,6]

And then I shuffle each list, each time I shuffle the list I want to print out how many matches in the list I received for example

lets say they get shuffled one time and this is the output

shuffled_list1 = 2,4,5,3,1,6
shuffled_list2 = 2,4,5,1,3,6

from the output I would like to receive 3 matches found.since the six came after a non match I do not want that to be counted.

Jfetto
  • 45
  • 4

3 Answers3

1

Try this -

l = zip(list1, list2)
matches = len(list(filter(lambda item:item[0] == item[-1], l)))

Output -

4
Zero
  • 1,800
  • 1
  • 5
  • 16
0
  • zip to build pair of items across different list(based on index)
  • break the loop if pair is not consisting same number
  • otherwise count
count = 0
for i,j in zip(shuffled_list1,shuffled_list2):
    if i!=j:
        break
    count+=1
Mazhar
  • 1,044
  • 6
  • 11
0

we suppose that list length are the same any time

count = 0
for _ , i  in list1:
    if list1[i] == list2[i]:
         count = count +1
    break
print(str(count)+"matches")
         
rusty
  • 63
  • 8