Most pythonic+computationally efficient way to check if each element in 2D array A is in any of the single arrays in 2D array B, and save the results

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.


This is not a numpy solution but works.

(i) Create a list of sets from B: B_sets

(ii) Iterate over A and in each iteration, check if an array in A is a superset of any set in B_sets. Depending on whether it is or not, construct a dictionary differently. Keep a list of sets in B_sets that were a subset of any set in A at least once.

(iii) Add the sets on B_sets that were never a subset of any set in A into the final output list: out

B_sets = [set(arr) for arr in B]

out = []
subset_B = []
for arr in A:
    out.append({})
    for b in B_sets:
        if set(arr).issuperset(b):
            out[-1]['label'] = 'Pass'
            subset_B.append(b)
            break
    else:
        out[-1]['label'] = 'Fail'
    out[-1]['pair'] = arr

out += [{'label': 'n/a', 'pair': np.array(list(b_set))} for b_set in B_sets if b_set not in subset_B]

Output:

[{'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([7, 4])},
 {'label': 'n/a', 'pair': array([2, 3])}]