Reputation: 53
I have this array:
array([[4798, 1369, 4139, ..., 2910, 2901, 0],
[ 2, 3161, 1343, ..., 2085, 2065, 2399],
[ 546, 506, 614, ..., 1874, 1859, 4799],
[ 0, 634, 1341, ..., 1886, 1871, 4799]], dtype=int64)
And I can see that there are two equal number (4799) in the last rows. That I want is make a function that compare the adjacent rows and return an array or list with the equal numbers.
I don't know how to do it. I try do it with a for
but when I have an largest array this method is very slow.
Upvotes: 0
Views: 100
Reputation: 56
I make the following assumptions to answer your question:
First, use the set()
function for the both rows you want to compare. Then use the intersection operator &
on both sets. It returns a new set with elements that are common to both sets Python Docs - 8.7. sets.
for i in range(len(array)):
# Start at i+1 to prevent double comparison of row pairs
for j in range(i+1, len(array)):
set_a = set(array[i])
set_b = set(array[j])
intersection = set_a & set_b
Upvotes: 1
Reputation: 224
Let's say a
is your array. Then try the following which may help,
def equalnumbers(a):
row,col = np.where((a[1:,:]-a[:-1,:]) ==0)
return a[row,col]
Trial example:
Note the array I use has two numbers that have an equal in their adjacent row. There's also the number 0 twice but in non adjacent rows!
import numpy as np
a = np.array([[4798, 1369, 4139, 2910, 2901, 0],
[4798, 3161, 1343, 2085, 2065, 2399],
[546, 506, 614, 1874, 1859, 4799],
[0, 634, 1341, 1886, 1871, 4799]])
equalnumbers(a)
Output:
array([4798, 4799])
The function has only returned the equal numbers from adjacent rows and not the 0 which is present in the non adjacent rows. Hope that helps.
Upvotes: 2