Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the identical pairs in two lists

My list has

a = [1,2,3,4,2,7,3,5,6,7]
b = [1,2,3,1,2,5,6,2,6,7]

I need to count if a[i]==b[i].

For the above example, the answer should be

6

Detail description of answer is

a[0]==b[0] (1==1)
a[1]==b[1] (2==2)
a[2]==b[0] (3==3)
a[4]==b[4] (2==2)
a[8]==b[8] (6==6)
a[9]==b[9] (7==7)
like image 692
Krush23 Avatar asked Jul 22 '19 11:07

Krush23


People also ask

How do you count the number of matches between two lists in Python?

The len() is used to return total count of match. The enumerate function can be used to produce the key value pair for the list being it's index and value and we can store them using list comprehension. The len() is used to return total count of match.

Are two lists identical Python?

Check Equality of Lists in Python Using the Equality == Operator. A straightforward way to check the equality of the two lists in Python is by using the equality == operator. When the equality == is used on the list type in Python, it returns True if the lists are equal and False if they are not.

How do you count a list in Python?

Len() Method There is a built-in function called len() for getting the total number of items in a list, tuple, arrays, dictionary, etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.


1 Answers

In a one-liner:

sum(x == y for x, y in zip(a, b))
like image 134
martyn Avatar answered Nov 15 '22 13:11

martyn