I have N number of list of same length. How can I compare these lists in corresponding position and output the number of positions where they all match?
For example:
A=[1,0,1,1]
B=[1,1,1,1]
C=[1,0,1,0]
comparison of these three list would output 2
as only position 1 and 3 matches.
I was thinking of converting it into tuple and then zipping it K=zip(A,B,C)
, then add each tuple to see if it matches the number of list.
The problem almost sounds like I am missing something that is rather trivial, maybe!
>>> A = [1, 0, 1, 1]
>>> B = [1, 1, 1, 1]
>>> C = [1, 0, 1, 0]
>>> [len(set(i)) == 1 for i in zip(A,B,C)]
[True, False, True, False]
>>> sum(len(set(i))==1 for i in zip(A,B,C))
2
sum(1 if x == y == z else 0 for x, y, z in zip(A, B, C))
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With