Passing a C++ std::Vector to numpy array in Python

前端 未结 4 692
走了就别回头了
走了就别回头了 2021-02-04 06:13

I am trying a pass a vector of doubles that I generate in my C++ code to a python numpy array. I am looking to do some downstream processing in P

4条回答
  •  北海茫月
    2021-02-04 06:41

      _import_array(); //this is required for numpy to create an array correctly
    

    Note: In Numpy's extension guide they use import_array() to accomplish the same goal that I used _import_array() for. When I tried using import_array(), on a mac I got an error. So you may need to try both commands and see which one works.

    By the way you can use C++ std::vector in the call to PyArray_SimpleNewFromData. If your std::vector is my_vector, replace fArraywith &my_vector[0]. &my_vector[0] allows you to access the pointer that stores the data in my_vector.

提交回复
热议问题