通过C模块中的Python API访问数组的数组
在 C 语言中,我们可以使用 Python 的 C API 来访问和操作数组的数组(即二维数组或嵌套列表)。通常,我们可以使用 Python C API 提供的 PyListObject
和 PySequence
相关函数来访问 Python 传递过来的列表结构。
1、问题背景
在 Python 中创建了一个包含数组的数组,并将其传递给 C 模块。我们需要通过 C 模块中的 Python API 访问此数组的每个子数组。
2、解决方案
为了访问传递给 C 模块的数组的数组,可以使用以下步骤:
- 在 C 模块中,使用
PyArray_SimpleNewFromData()
函数创建一个新的 NumPy 数组。此函数将创建一个新的数组,并使用提供的数据填充它。 - 将传递给 C 模块的数组的数组的元素复制到新创建的数组中。这可以使用
PyArray_CopyInto()
函数来完成。 - 使用
PyArray_NDIM()
函数获取新创建的数组的维度数。 - 使用
PyArray_SHAPE()
函数获取新创建的数组的形状。 - 使用
PyArray_GETPTR1()
函数获取新创建的数组的数据指针。 - 使用数据指针访问新创建的数组中的元素。
以下是一个代码示例:
# C 代码
# 包含 NumPy 头文件
# conda install numpy
# pip install numpy
# apt-get install python3-numpy (For Debian / Ubuntu)
# yum install python3-numpy (For CentOS / Red Hat)
# dnf install python3-numpy (For Fedora)
# zypper install python3-numpy (For OpenSUSE)
# Include the necessary header files
# For Windows: Python.h must be placed in the project directory
# For Linux: python3.X/Include/Python.h
# For macOS: /Library/Frameworks/Python.framework/Versions/3.X/Headers/Python.h
# https://www.python.org/dev/embedding/
# https://www.oreilly.com/library/view/python-c-extension/0596001566/re314.html
# https://docs.python.org/3/c-api/array.html
# https://docs.scipy.org/doc/numpy/reference/c-api/c-api-intro.html
// https://stackoverflow.com/questions/5390264/numpy-array-of-objects-passing-to-c-and-access-to-these-objects-in-c
# https://docs.scipy.org/doc/numpy-1.15.1/reference/c-api/ndarray.html
# https://github.com/numpy/numpy/issues/11485
# https://github.com/numpy/numpy/issues/2662
# https://www.oreilly.com/library/view/python-c-extension/0596001566/re335.html
# https://stackoverflow.com/questions/47386916/how-to-create-a-numpy-array-of-objects-in-c-extension
// https://cs231n.github.io/python-numpy-tutorial/
# https://jonathannienaber.de/programming/python-native-memoryview-efficient-cpython-extension-types/
# https://numpy.org/doc/stable/c-api/c-api-reference.html
# https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html
# https://numpy.org/doc/stable/user/basics.types.html
// https://stackoverflow.com/questions/37056848/how-to-pass-a-numpy-array-from-python-to-c
// https://numpy.org/doc/stable/reference/c-api/shape.html
// https://stackoverflow.com/questions/68689338/convert-numpy-array-of-bytes-into-list-of-strings
# https://numpy.org/doc/stable/reference/c-api/array.html
// https://numpy.org/doc/stable/user/basics.creation.html#creating-arrays
# https://numpy.org/doc/stable/reference/c-api/dtype.html
# https://numpy.org/doc/stable/reference/c-api/dtype_buffer.html
# https://numpy.org/doc/stable/reference/c-api/dtype_flag.html
# https://scipython.com/blog/interacting-between-python-and-c-data-structures/
# 定义必要的变量和数据结构
PyObject *PyArray, *PySubArray;
int PyNumDims = 0;
npy_intp *PyShape = NULL;
PyArray_Descr *PyDtype = NULL;
void *PyDataPtr = NULL;
// 将传递给 C 模块的数组的数组转换为兼容的 NumPy 数组
PyObject *PyArrayConvert(PyObject *PyArray) {
// 检查输入数组是否为数组对象
if (!PyArray_Check(PyArray)) {
PyErr_SetString(PyExc_TypeError, "Input must be a NumPy array");
return NULL;
}
// 检查输入数组是否是数组的数组
PyNumDims = PyArray_NDIM(PyArray);
if (PyNumDims != 2) {
PyErr_SetString(PyExc_TypeError, "Input must be a 2-dimensional array");
return NULL;
}
// 检查输入数组的元素类型是否兼容
PyDtype = PyArray_DESCR(PyArray);
if (PyDtype->type_num != NPY_OBJECT) {
PyErr_SetString(PyExc_TypeError, "Input array must have object data type");
return NULL;
}
// 将输入数组转换为兼容的 NumPy 数组
PyArray = PyArray_FromArray(PyArray, PyArray_DescrFromType(NPY_OBJECT), NPY_ARRAY_DEFAULT);
if (PyArray == NULL) {
PyErr_SetString(PyExc_RuntimeError, "Failed to convert input array");
return NULL;
}
return PyArray;
}
// 从兼容的 NumPy 数组中提取数组的数组元素
PyObject **PyExtractSubArrays(PyObject *PyArray) {
// 获取兼容的 NumPy 数组的形状和数据指针
PyShape = PyArray_SHAPE(PyArray);
PyDataPtr = PyArray_DATA(PyArray);
// 分配内存来存储提取的数组的数组元素
PyObject **PySubArrays = malloc(PyShape[0] * sizeof(PyObject *));
if (PySubArrays == NULL) {
PyErr_SetString(PyExc_MemoryError, "Failed to allocate memory for sub-arrays");
return NULL;
}
// 提取兼容的 NumPy 数组的数组的数组元素
for (int i = 0; i < PyShape[0]; i++) {
PySubArrays[i] = ((PyObject **)PyDataPtr)[i];
}
return PySubArrays;
}
// 释放提取的数组的数组元素的内存
void PyFreeSubArrays(PyObject **PySubArrays) {
free(PySubArrays);
}
// 在 C 模块中访问提取的数组的数组元素
void PyAccessSubArrays(PyObject **PySubArrays) {
// 循环访问提取的数组的数组元素
for (int i = 0; i < PyShape[0]; i++) {
// 访问提取的数组的数组元素的元素
for (int j = 0; j < PyShape[1]; j++) {
// 根据需要访问提取的数组的数组元素的元素的值
// printf("%d\n", ((int **)PySubArrays[i])[j]);
}
}
}
// 在 C 模块中释放 kompatibler NumPy 数组的内存
void PyFreeArray(PyObject *PyArray) {
Py_DECREF(PyArray);
}
请注意,此代码假设传递给 C 模块的数组的数组是一个二维数组,并且它的元素是对象。如果数组的数组具有不同的维度或元素类型,则需要修改代码以支持不同的情况。
以下是一个 Python 代码示例,演示如何使用上述 C 代码访问数组的数组:
# Python 代码
# 导入必要的库
import numpy as np
import geoms
# 创建一个包含数组的数组
A=np.empty((1,2),dtype=object)
A[0,0] = np.random.rand(3,3)
A[0,1] = np.random.rand(5,5)
# 将数组的数组传递给 C 模块
geoms.gm_unique_all(A)
通过这种方式,C 代码可以访问 Python 传递的二维数组(列表的列表),并在 C 端处理数据后返回新的 Python 结构。