pythonlistmultidimensional-array

How to find only two matches & non-matches in multiple 2d list?


I have two 2d list with large amount of rows and different size length for both lists. I'm having trouble with finding only two matches in each sub-list in df1 and df2. Let say I would like to find two matches from df1 in df2 I would like to return the matching values and non-matching values. which will probably convert into a 3d list then I would like to remove the bracket and convert it back to 2d list. If there's a way to convert into 2d list without converting into 3d list first would help me out a lot.

Main List
df1 = [[1, 7, 3, 5], [2, 5, 14, 10], [1,14,25,34,67,74], [2,19,45,65]]
df2 = [[1, 17, 3, 5], [34, 14, 74], [34, 3, 87], [25, 14, 10], [2,19,20,64,25]]


Desire Result (Remove Brackets and convert back to 2d list).
   match = [[10,14], [14,25], [14,25], [2,19]]
   no_match = [[25], [10],[25], [20,64,25]]

I have tried this code below. But I'm only trying to get only two matches and return the matching and non_matching values.

no_matches = []

for sublist1 in df1:
    for sublist2 in df2:
        match = [val for val in sublist1 if val in sublist2]
        no_match = [val for val in sublist2 if val not in sublist1]
        
        if match: matches.append(match)
        if no_match: no_matches.append(no_match)

print("matches:", matches)
print("no matches:", no_matches)

Solution

  • May be you need something like this (assumes each sub list has only unique values):

    match = []
    no_match = []
    
    for i in df1:
        for j in df2:
            set_i = set(i)
            set_j = set(j)
            intersection = set_i & set_j
            if len(intersection) == 2:
                match.append(list(intersection))
                no_match.append(list(set_j - set_i))