I’m in need of a method to quickly return the number of differences between two large lists. The contents of each list item is either 1 or 0 (single integers), and the amount of items in each list will always be 307200.
This is a sample of my current code:
list1 = <list1> # should be a list of integers containing 1's or 0's
list2 = <list2> # same rule as above, in a slightly different order
diffCount = 0
for index, item in enumerate(list1):
if item != list2[index]:
diffCount += 1
percent = float(diffCount) / float(307200)
The above works but it is way too slow for my purposes. What I would like to know is if there is a quicker way to obtain the number of differences between lists, or the percentage of items that differ?
I have looked at a few similar threads on this site but they all seem to work slightly different from what I want, and the set() examples don’t seem to work for my purposes. 😛
You can get at least another 10X speedup if you use NumPy arrays instead of lists.