[Python] Modify list of arrays in place

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a df like:

    # /// script
    # requires-python = ">=3.13"
    # dependencies = [
    # "polars",
    # ]
    # ///

    import polars as pl

    df = pl.DataFrame(
    {
    "points": [
    [
    [1.0, 2.0],
    ],
    [
    [3.0, 4.0],
    [5.0, 6.0],
    ],
    [
    [7.0, 8.0],
    [9.0, 10.0],
    [11.0, 12.0],
    ],
    ],
    },
    schema={
    "points": pl.List(pl.Array(pl.Float32, 2)),
    },
    )
    """
    shape: (3, 1)
    ┌─────────────────────────────────┐
    │ points │
    │ --- │
    │ list[array[f32, 2]] │
    ╞═════════════════════════════════╡
    │ [[1.0, 2.0]] │
    │ [[3.0, 4.0], [5.0, 6.0]] │
    │ [[7.0, 8.0], [9.0, 10.0], [11.… │
    └─────────────────────────────────┘
    """


    Each point represents an (x, y) pair. How can I divide all x by 2, and all y by 4.

    shape: (3, 1)
    ┌─────────────────────────────────┐
    │ points │
    │ --- │
    │ list[array[f32, 2]] │
    ╞═════════════════════════════════╡
    │ [[0.5, 0.5]] │
    │ [[1.5, 1.0], [2.5, 1.5]] │
    │ [[3.5, 2.0], [4.5, 2.5], [5.5,… │
    └─────────────────────────────────┘

    Continue reading...

Compartilhe esta Página