您的位置:首页 > 科技 > IT业 > 宣传册内容排版_前端开发的三大基石_建立免费个人网站_自助建站网站模板

宣传册内容排版_前端开发的三大基石_建立免费个人网站_自助建站网站模板

2025/2/25 11:59:15 来源:https://blog.csdn.net/weixin_44617651/article/details/145510879  浏览:    关键词:宣传册内容排版_前端开发的三大基石_建立免费个人网站_自助建站网站模板
宣传册内容排版_前端开发的三大基石_建立免费个人网站_自助建站网站模板

在 C 语言中,我们可以使用 Python 的 C API 来访问和操作数组的数组(即二维数组或嵌套列表)。通常,我们可以使用 Python C API 提供的 PyListObjectPySequence 相关函数来访问 Python 传递过来的列表结构。

在这里插入图片描述

1、问题背景

在 Python 中创建了一个包含数组的数组,并将其传递给 C 模块。我们需要通过 C 模块中的 Python API 访问此数组的每个子数组。

2、解决方案

为了访问传递给 C 模块的数组的数组,可以使用以下步骤:

  1. 在 C 模块中,使用 PyArray_SimpleNewFromData() 函数创建一个新的 NumPy 数组。此函数将创建一个新的数组,并使用提供的数据填充它。
  2. 将传递给 C 模块的数组的数组的元素复制到新创建的数组中。这可以使用 PyArray_CopyInto() 函数来完成。
  3. 使用 PyArray_NDIM() 函数获取新创建的数组的维度数。
  4. 使用 PyArray_SHAPE() 函数获取新创建的数组的形状。
  5. 使用 PyArray_GETPTR1() 函数获取新创建的数组的数据指针。
  6. 使用数据指针访问新创建的数组中的元素。

以下是一个代码示例:

# 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 结构。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com