Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare corresponding positions in a list?

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!

like image 302
msakya Avatar asked Sep 16 '25 19:09

msakya


2 Answers

>>> 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
like image 159
John La Rooy Avatar answered Sep 18 '25 08:09

John La Rooy


sum(1 if x == y == z else 0 for x, y, z in zip(A, B, C))
2
like image 27
iruvar Avatar answered Sep 18 '25 10:09

iruvar