Raldenors
Raldenors

Reputation: 311

Count the number with same value in the matrix using Python

I have this data:

x = [1,0,1,0,1]
y = [0,0,1,0,0]

Now, I want to match the x and y matrices. If each data have the same value, the program will count it as 1, if not then zero. Then after it counts the data, it will get the sum. The output is;

Output: 3

So, to explain, the first column of x and y do not match (1 and 0), then the next three data (0 and 0), (1 and 1), and (0 and 0) are the same value. So, if we will count it, the sum is 3.

Thanks in advance!

Upvotes: 0

Views: 456

Answers (2)

Anake
Anake

Reputation: 7669

If you have numpy installed, you can do the following:

In [3]: import numpy as np

In [4]: x = np.array(x)

In [5]: y = np.array(y)

In [7]: (x == y).sum()
Out[7]: 3

Upvotes: 1

user3645016
user3645016

Reputation: 673

Without using any additional libraries, one thing you could do is:

sum(l == r for l, r in zip(x, y))

Upvotes: 1

Related Questions