Consider arrays m and n. Both have identical shapes. m and n always have an even number of columns, and I have added spaces to emphasize that each array row is made up of PAIRS of elements.
import numpy as np
m = np.array([[5,3, 6,7, 3,8],
[5,4, 5,1, 4,5],
[5,4, 2,4, 4,6],
[2,2, 2,3, 8,5],
[2,7, 8,7, 1,2],
[2,7, 8,7, 3,2],
[2,7, 8,7, 4,2]])
n = np.array([[1,3, 6,7, 1,12],
[2,4, 2,9, 4,9],
[1,5, 5,12, 9,1],
[5,4, 5,6, 9,5],
[5,4, 1,4, 1,5],
[5,4, 1,7, 9,5],
[5,4, 1,5 4,11]])
My overall objective is to eliminate rows from m that fail to meet two TEST conditions:
TEST 1: we keep rows of m only if every pair had a common element with some other pair in the row. This has already been answered very capably by Quang Hoang on Nov 13, 2020, (https://stackoverflow.com/a/64814379/11188140 ). I include it here because the code will be helpful with TEST 2 to follow.
Using this code, the 1st row of m is rejected because pair (6,7) has no common element in the other row pairs. The 4th row of m also is rejected because its last pair has no common element in the other row pairs. The earlier code, that works perfectly, and its output are as follows:
a = m.reshape(m.shape[0],-1,2)
mask = ~np.eye(a.shape[1], dtype=bool)[...,None]
is_valid = (((a[...,None,:]==a[:,None,...])&mask).any(axis=(-1,-2))
|((a[...,None,:]==a[:,None,:,::-1])&mask).any(axis=(-1,-2))
).all(-1)
m[is_valid]
The output, m, is:
[[5,4, 5,1, 4,5],
[5,4, 2,4, 4,6],
[2,7, 8,7, 1,2],
[2,7, 8,7, 3,2],
[2,7, 8,7, 4,2]]
TEST 2: we keep rows of m only if the row in array n (ie: same index) has the SAME PAIR MATCHINGS as in m. An n row may have 'extra' pair matchings as well, but it MUST include the pair matchings that m has.
Three examples make this clear:
a) 5th row of m and n: '[2,7, 8,7, 1,2]' and '[5,4, 1,4, 1,5]'
In m, the 1st & 2nd pairs share an element, and the 1st and & 3rd pairs share an element.
n has both of these matchings, so **TEST 2 PASSES**, and we keep this m row.
The fact that 2nd & 3rd pairs of n also share an element is immaterial.
b) 6th row of m and n: '[2,7, 8,7, 3,2]' and '[5,4, 1,7, 9,5]'
In m, the 1st & 2nd pairs share an element, and the 1st & 3rd pairs share an element.
n DOES NOT have BOTH of these matchings, so **TEST 2 FAILS**. The row may be eliminated from m (and from n too if that's needed)
c) 3rd row of m and n: '[5,4, 2,4, 4,6]' and '[1,5, 5,12, 9,1]'
In m, the 1st & 2nd pairs share an element, the 2nd & 3rd pairs share an element, and the 1st & 3rd pairs share an element.
n lacks the 2nd & 3rd pair sharing that m has, so **TEST 2 FAILS**. The row may be eliminated from m (and from n too if that's needed)
The final output, m, after passing both tests, is:
[[5,4, 5,1, 4,5],
[2,7, 8,7, 1,2],
[2,7, 8,7, 4,2]]