[Python] Fast way to expand split list into index list

Discussão em 'Python' iniciado por Stack, Março 12, 2025.

  1. Stack

    Stack Membro Participativo

    Given an index split list T of length M + 1, where the first element is 0 and the last element is N, generate an array D of length N such that D[T:T[i+1]] = i.

    For example, given T = [0, 2, 5, 7], then return D = [0, 0, 1, 1, 1, 2, 2].

    I'm trying to avoid a for loop, but the best I can do is:

    def expand_split_list(split_list):
    return np.concatenate(
    [
    np.full(split_list[i + 1] - split_list, i)
    for i in range(len(split_list) - 1)
    ]
    )


    Is there a built-in function for that purpose?

    Continue reading...

Compartilhe esta Página