Skip to content

Convert vector of string to numpy array #1775

@damondd

Description

@damondd

If the max length of string is known in advance, I can write code like:

std::vector<std::string> v = {"1", "23", "456"};
using S8 = char[8];
py::array_t<S8> arr(v.size());
auto pdest = static_cast<S8*>(arr.request().ptr);
for (std::size_t i = 0; i < v.size(); ++i) {
  std::strncpy(pdest[i], v[i].data(), 8);
}

But what if the vector is created dynamically and the longest string is unknown? Please advise.

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