Skip to content

question #318

Closed
Closed

Description

@tuoeg

PyReadonlyArray2 how to convert to vec<vec>? thanks

Activity

  1. adamreichold commented on Apr 12, 2022

    @adamreichold
    Member

    I think the easiest way to achieve this is to use ndarray's rows or Columns iterators and collect those, e.g.

    fn array2_to_vec2(array: PyReadonlyArray2<f64>) -> Vec<Vec<f64>> {
        array
            .as_array()
            .rows()
            .into_iter()
            .map(|row| row.iter().copied().collect::<Vec<_>>())
            .collect::<Vec<_>>()
    }

    But that said, this is an usual conversion as the resulting memory layout is relatively inefficient compared to store the two-dimensional array in a single allocation. Maybe you could expand a little on why you need Vec<Vec<_>> and we can find a way to reduce the number of separate allocations?

  2. locked and limited conversation to collaborators on Apr 12, 2022
  3. converted this issue into a discussion #320 on Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions