如何将python dict发送到C ++

问题描述

  • 这是我的python函数:
if __name__ == "__main__":
    file = open("data_input.txt","r")
    contents = file.read()
    e = ast.literal_eval(contents)
    neighbour_num= 4
    new_data = 300
    result = np.ndarray((4,4),dtype='int32',buffer=np.zeros((4,dtype='int32'))
    c_module.mmult_wrapper(e,new_data,neighbour_num,result)
  • 这是我的C ++函数,我想在其中使用dict作为数组
   void mmult(double new_data,double neighbour_num,double e[100],int32_t result[16]) {
   double distances[100];
   unsigned distances_front = 0;
   unsigned distances_back = 0;

   double keys[100];
   unsigned keys_front = 0;
   unsigned keys_back = 0;
 

   for(int i=0;i<(int)100; i=i+1) {

      double temp_distance=(new_data-e[i]);

      double sq_tmp=(temp_distance*temp_distance);

      double sqrt=(sq_tmp/2);

      double temp=0;
      while(sqrt!=temp) {
         temp = sqrt;
         sqrt = (((sq_tmp/temp)+temp)/2);
      }
      distances[distances_front++] = (sqrt);
      keys[keys_front++] = (e[i-1]);
   }
   for(int i=0; i<(int)(keys_front - keys_back); i=i+1) {
      for(int j=(i+1); j<(int)(distances_front - distances_back); j=j+1) {
         if(distances[i]>distances[j]) {
            distances[i] = distances[j];
            distances[j] = distances[i];
            keys[i] = keys[j];
            keys[j] = keys[i];
         }
      }
   }
   for(int i=0; i<(int)neighbour_num; i=i+1) {
      result[i] = keys[i];
   }
} 
  • 这是我拥有的包装函数
#include <Python.h>
#include <numpy/arrayobject.h>
#include "spyc.h"
#include "caller.h"

static PyObject* mmult_wrapper(PyObject* self,PyObject* args) {

   int32_t e;
   int32_t new_data;
   int32_t neighbour_num;
   int32_t result;

   int res = PyArg_ParseTuple(args,"Oi",&e_obj,&d);

   if (!res)
      return NULL;


   /* call function */
   mmult_caller(e,d);

} 

  • 我的目标是接受来自python函数的dict值并将其转换为适合包装器中C的数组值。我没有C \ C ++的先验知识,被困住了。任何帮助将大有帮助。谢谢

解决方法

您可能需要包括dictobject.h,floatobject.h,(如果尚未通过其他标头插入)。这是一种从浮点数dict到向量的所有值的方法。根据dict中的实际内容和结构,您可能需要查看按键和其他部分。 请注意,这是C ++,因此应这样编译。


//returns true on success
bool dictToVector(PyObject *  srcDict,std::vector<double> & destVector) {
 destVector.clear();
 if(PyDict_Check(srcDict)) {
  Py_ssize_t numItems = PyDict_Size(srcDict);
  destVector.reserve(numItems);
  Py_ssize_t iteratorPPOS = 0;
  PyObject * currentVal;
  while(PyDict_Next(srcDict,&iteratorPPOS,NULL,&currentVal) {
   // might be worth checking PyFloat_Check...
   destVector.push_back(PyFloat_AsDouble(currentVal));
  }
  return (numItems == destVector.size());
 }
 else { // not a dict return with failure
  return false;
 }
}

最后,您的包装器将类似,因为它需要将mmult的结果复制到结果类型numpy.ndarray中。

上述用法的用法:


PyObject* mmult_wrapper(PyObject * e,PyObject * new_data,PyObject * neighbour_num,PyObject * result) {
 int32_t Cresult[16];
 std::vector<double> Ce;
 bool noErrorSoFar = dictToVector(e,Ce);
 if(Ce.size() == 100) {
  mmult(PyFloat_AsDouble(new_data),PyFloat_AsDouble( neighbour_num),Ce.data(),Cresult);
 }
else { // not 100 doubles in the data read!
  noErrorSoFar = false;
 }

... //some stuff to copy Cresult to the python,and return something meaningful?
}

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...