If I have two list such as
list1 = ['cat', 'sat', 'on', 'mat', 'xx', 'yy'] ,
list2 = ['cow', 'sat', 'on', 'carpet', 'xx', 'yy']
I have walk along the lists: when I see two elements that match, start counting. when I see another pair of elements that don't match, stop that counter and start another one.
(sat, sat) I = 1
(on, on) I = 2
(mat, carpet) J = 1
(xx, xx) k = 1
(yy, yy) k = 2
i = 0
for x in list1:
for y in list2:
if x == y:
print (x, y)
i += 1
else:
j = 0
j += 1
print (x, y)