[Python] How to match three 3d list

Discussão em 'Python' iniciado por Stack, Junho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have three 3d list below. I would like to find a match in the same row as B & C that matches A then return only the matching values. For example, if A finds a match in B in row 3 also has to be the same row in C. Here is my 3d list below.

    A = [[11, 27, 41, 42], [7, 26, 34, 36], [8,22, 24, 38, 42]]
    B = [[11, 42], [11, 32], [7, 14], [11, 17, 21], [21, 38], [11, 42,45]]
    C = [[15, 16, 21, 27, 38, 44], [34, 39, 42], [20, 21, 35, 36, 49], [2, 12, 22, 24, 30], [21, 22, 27, 31, 42], [1, 5, 8, 27, 32, 38]]


    I have tried this code below, but it's doesn't work the way I would like.

    match = []

    for e in A:
    for f in B:
    for g in C:
    set_e = set(e)
    set_f = set(f)
    set_g = set(g)
    h = set_i & set_j | set_i & set_z
    if len(h) > 0:
    match.append(list(h))

    print(match)


    I would like my result to be like this. The matching values in the same row as B & C

    match = [[11, 42, 27], # 11,42 in A & B and 27 in A & C in row 0
    [11, 42], # 11 in A & B and 42 in A & C in row 1
    [11, 42, 27], # 11,42 in A & B and 27 in A & C in row 5
    [[42, 38], # 42 in A & B and 38 in A & C in row 0
    [38, 42, 22], # 38 in A & B and 42, 22 in A & C in row 4
    [42, 38, 8]] # 42 in A & B and 38 in A & C in row 5

    Continue reading...

Compartilhe esta Página