Search code examples
pythonlistmultidimensional-arrayjupyter-notebookpycharm

How to match two 2d list


I'm having trouble finding find a match in any columns in df1 that match any columns in df2 and return the matching values and values. For example,

df1 = [[2,4,6,8,9,10],[10,13,15,17,26,44],[27,28,34,37,40]]

df2 = [[1,2,4,5,6,8],[5,6,20,22,23,34],[8,12,13,34,45,46],[9,10,14,29,32,33],[1,22,13,23,33,35],[1,6,7,8,9,10],[0,2,3,5,6,8]] 

I would like my results to be like this below

 result = [[1,2,2,4,5,6,6,8,8,9,10],
[2,4,5,6,6,8,9,10,20,22,23,34],
[2,4,6,8,8,9,10,12,13,34,45,46]
[2,4,6,8,9,9,10,10,14,29,32,33]
[1,2,4,6,6,7,8,8,9.10,10]
[0,2,2,3,4,5,6,6,8,8,9,10]],

[[8,10,12,13,13,15,17,26,34,44,45,46],
[9,10,10,13,14,15,17,26,29,32,33,44],
[1,6,7,8,9,10,10,13,15,17,26,44]],

[[5,6,20,22,23,,27,28,34,34,37,40]
[8,12,13,27,28,34,37,40,45,46]]

Solution

  • You could use sets to establish if there is match and concatenate/sort the subllists to the output lists.

    I couldn't get exactly your expected result, but close (perhaps [1, 10, 13, 13, 15, 17, 22, 23, 26, 33, 35, 44] on the second groups was missing from your example):

    result = [ [sorted(c1+c2) for c2 in df2 if set(c1)&set(c2)] for c1 in df1 ]
    
    for group in result:
        print(*group,sep="\n")
        print()
    
    [1, 2, 2, 4, 4, 5, 6, 6, 8, 8, 9, 10]
    [2, 4, 5, 6, 6, 8, 9, 10, 20, 22, 23, 34]
    [2, 4, 6, 8, 8, 9, 10, 12, 13, 34, 45, 46]
    [2, 4, 6, 8, 9, 9, 10, 10, 14, 29, 32, 33]
    [1, 2, 4, 6, 6, 7, 8, 8, 9, 9, 10, 10]
    [0, 2, 2, 3, 4, 5, 6, 6, 8, 8, 9, 10]
    
    [8, 10, 12, 13, 13, 15, 17, 26, 34, 44, 45, 46]
    [9, 10, 10, 13, 14, 15, 17, 26, 29, 32, 33, 44]
    [1, 10, 13, 13, 15, 17, 22, 23, 26, 33, 35, 44] # <-- extra
    [1, 6, 7, 8, 9, 10, 10, 13, 15, 17, 26, 44]
    
    [5, 6, 20, 22, 23, 27, 28, 34, 34, 37, 37, 40]
    [8, 12, 13, 27, 28, 34, 34, 37, 37, 40, 45, 46]
    

    Note that using sets assumes that you don't have duplicate values in the sublists. If you do, then you would need to use the Counter class from collections instead of set.