[Python] How can I explode the string in sublists and fill into individual sublist

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list like this.

    [["Hi, this is Tesa form the sales departmet. Iam working here from"],[""],[""]]


    How can I explode this string inside the first sublist by preserving words by filling all sublists.Like,

    [["Hi, this is Tesa."],["form the sales departmet"],["Iam working here from"]]


    Things to consider:

    1. The number of sublists will can be n but all of them will be empty for sure except the first one.
    2. Word format should not be broken. There is no need to preserve the sentence meaning.

    what I have:

    def explode_string(array, split_positions=None):
    original_text = array[0][0]
    num_sublists = len(array)

    if split_positions is None:
    chunk_size = len(original_text) // num_sublists
    split_positions = [chunk_size * i for i in range(1, num_sublists)]

    parts = []
    start = 0
    for pos in split_positions:
    parts.append(original_text[start:pos])
    start = pos
    parts.append(original_text[start:])

    result = [[part] for part in parts]

    return result

    original_array = [
    ["Hi, this is Tesa form the sales departmet. Iam working here from"],
    [""],
    [""]
    ]

    result = explode_string(original_array, [15, 39])
    print(result)


    The output iam getting:

    [['Hi, this is Tes'], ['a form the sales departm'], ['et. Iam working here from']]


    The dataset is very long, Looking if you guys suggest any easy approch to tackle this.

    Continue reading...

Compartilhe esta Página