Say that I have these two 2d arrays
A = np.array(
[[1, 3],
[1, 4],
[1, 7],
[3, 4],
[3, 7],
[7, 4]]
)
B = np.array(
[[2, 3],
[4, 7]]
)
I want to record if each 1D array in A contains both elements of any of the 1d arrays in B.
For clarity, the order is not important in any case.
I also want to record if there were any 1D arrays where both elements in B were not in any of the 1D arrays in A.
Here is an example ideal result:
[{'label': 'Fail', 'pair': array([1, 3])},
{'label': 'Fail', 'pair': array([1, 4])},
{'label': 'Fail', 'pair': array([1, 7])},
{'label': 'Fail', 'pair': array([3, 4])},
{'label': 'Fail', 'pair': array([3, 7])},
{'label': 'Pass', 'pair': array([4, 7])},
{'label': 'n/a', 'pair': array([2, 3])}]
array([4, 7])
has a pass
because each element in that 1D array in B is contained in one of the 1D arrays in A.
array([2, 3])
has a n/a
because each element in that 1D array in B is not contained in one of the 1D arrays in A.
The rest have a Fail
because each element in those 1D arrays in A is Not contained any of the 1D arrays in B.
This is my best attempt
record = []
B_leftovers = set()
for idxA, A_comb in enumerate(A):
fulfilled = False
for idxB, B_comb in enumerate(B):
if B_comb[0] in A_comb and B_comb[1] in A_comb:
record.append(
{'pair': A_comb,
'label': 'Pass'}
)
fulfilled = True
if frozenset([B_comb[0], B_comb[1]]) in B_leftovers:
B_leftovers.remove(frozenset([B_comb[0], B_comb[1]]))
continue
else:
B_leftovers.add(frozenset([B_comb[0], B_comb[1]]))
if fulfilled == False:
record.append(
{'pair': A_comb,
'label': 'Fail'}
)
for item in B_leftovers:
first, second = list(item)
record.append(
{'pair': np.array([first, second]),
'label': 'n/a'}
)
record
The solution seems a little convoluted, tricky to read, and may not be most computationally efficient due to the double loop.
This question is different from other recommended questions similar to this because the order or the elements is not relevant and each 1D array in both 2D arrays is being compared to each other.