[英]How to implement Python arrays in a compiled C module using the Python C API
[英]How to access to an array of arrays using python APIs in C module
我需要訪問從python傳遞到C模塊的數組數組。 例如,假設在python中創建了一個數組“ A”的數組,如下所示:
from numpy import *
from numpy.random import rand
from geoms import *
A=empty((1,2),dtype=object)
A[0,0]=rand(3,3)
A[0,1]=rand(5,5)
gm_unique_all(A)
其中geoms是在python中設置和導入的C模塊,而gm_unique_all是該方法的一種。 我不知道如何通過C的python API訪問輸入數組“ A”的每個子數組。如果每個人都可以幫助我,我將非常感激。
大概是這樣的:
void processMatrix(PyObject *mat) {
Py_ssize_t r, c;
if (!PySequence_Check(mat))
; // mat is not a sequence
for (r=0; r<PySequence_Length(mat); ++r) {
PyObject *row = PySequence_GetItem(mat, r);
if (!PySequence_Check(row))
; // row is not a sequence
for (c=0; c<PySequence_Length(row); ++c) {
PyObject *obj = PySequence_GetItem(row, c);
// process obj
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.