I have two lists containing 1's and 0's, e.g.
list1 = [1,1,0,1,0,1] list2 = [0,1,0,1,1,0]
I want to find the number of times they match at each index. So in this case the output would be 3 because they have the same value at indices 1,2 and 3 only.
Currently I'm doing this:
matches_list =  for i in list1: index = list.index(i) if list1[index] == list2[index]: mathes_list.append(i) else: pass return len(matches_list)
However this is very slow and I want to do this many times over to compare a large number of these lists
I was hoping someone could advise me on a quicker way to do this. Is there a way to use the set() function, or something similar, for example to compare two lists but maintain the order of each one?
zip the lists, compare the elements, compute the sum.
>>> list1 = [1,1,0,1,0,1] >>> list2 = [0,1,0,1,1,0] >>> sum(a == b for a,b in zip(list1, list2)) 3
itertools.izip in Python 2 for memory efficiency.)