[Python] Concatenate arrays row-wise when sub-arrays overlap

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

  1. Stack

    Stack Membro Participativo

    I have a dataset that roughly has the following structure:

    arrays = [
    np.array([['a', 'b'], ['', ''], ['', ''], ['a', 'b'], ['a', 'b'], ['', '']]),
    np.array([['', ''], ['b', 'c'], ['', ''], ['b', 'c'], ['', ''], ['', '']]),
    np.array([['', ''], ['c', 'd'], ['c', 'd'], ['', ''], ['', ''], ['', '']]),
    np.array([['d', 'a'], ['d', 'a'], ['', ''], ['', ''], ['d', 'a'], ['', '']]),
    np.array([['', ''], ['', ''], ['e', 'b'], ['e', 'b'], ['e', 'b'], ['', '']])
    ]


    I already stacked and transposed them to look like this:

    stacked = np.stack(arrays)
    stacked = np.transpose(stacked, (1, 0, 2))

    [[['a' 'b'], ['' ''], ['' ''], ['d' 'a'], ['' '']],
    [['' ''], ['b' 'c'], ['c' 'd'], ['d' 'a'], ['' '']],
    [['' ''], ['' ''], ['c' 'd'], ['' ''], ['e' 'b']],
    [['a' 'b'], ['b' 'c'], ['' ''], ['' ''], ['e' 'b']],
    [['a' 'b'], ['' ''], ['' ''], ['d' 'a'], ['e' 'b']],
    [['' ''], ['' ''], ['' ''], ['' ''], ['' '']]]


    I want to 'group' all arrays that have overlapping data. So ['a', 'b'] and ['b', 'c'] should together become ['a', 'b', 'c'] etc.

    I can't seem to find any efficient and readable way to do this. Any tips?

    Continue reading...

Compartilhe esta Página