def compare_two_lists(list1,list2):
i=0
j=0
while i < len(list2) :
if i%2 == 0:
j == 0
else:
j == 1
for sublist2 in list2[i:] :
for sublist in list1[j:]:
#sublist.intersection(sublist2)
intersect = [x for x in sublist if x in sublist2]
print('from set ',sublist, len(intersect),' matched number(s): ', intersect)
i=i +1
compare_two_lists([[1,2,3,4,5],[20,30]],[[5,3,7,8,1],[20,10],[4,10,1,7,8],[30,20]])
I am trying to get list 0 and 1 of list 1 to compare appropriately list 0, 1, 2 and 3 of list 2 and return matches. The program almost works in the sense that it does return matches for the lists amongst other iterations. I cannot seem to be able to get the iteration to occur twice and return [1,3,5],[20], [1,4],[20,30]. Please help. I am going quite mad trying to understand how to space functions correctly and use loops logically!!
This seems to do the trick. Here’s an explanation (see line labels above):
j==1andj==0in your code. These operators don’t change any operand values. you meantj=1etc. but this way is quickerFor explaining the rest I’ll just talk through the actual iteration with your example input lists:
i = 0 (the first iteration)
i=1
i=3