当前位置: 首页 > article >正文

通过C模块中的Python API访问数组的数组

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


http://www.kler.cn/a/539447.html

相关文章:

  • 2025影视泛目录站群程序设计_源码二次开发新版本无缓存刷新不变实现原理
  • 对话框补充以及事件处理机制 (2025.2.10)
  • (2024|Nature Medicine,生物医学 AI,BiomedGPT)面向多种生物医学任务的通用视觉-语言基础模型
  • postgresql 游标(cursor)的使用
  • git fetch和git pull 的区别
  • 【C++】 STL -- 算法(二)
  • Playwright 与 Selenium 的关系
  • java基础5(黑马)
  • 【kafka实战】05 Kafka消费者消费消息过程源码剖析
  • Kotlin 2.1.0 入门教程(十一)for、while、return、break、continue
  • 捕获一只比特币挖矿木马
  • vllm 部署 qwen2.5 报错2.5 报错404 已解决
  • java基础语法中阶
  • Docker Compose 容器卷映射:是否需要提前将文件拷贝到宿主机?
  • 【论文阅读笔记】HiDDeN:Hiding Data With Deep Networks
  • vue3中使用print-js组件实现打印操作
  • 蓝桥杯51单片机练习(国信长天比赛用)
  • c/c++蓝桥杯经典编程题100道(18)括号匹配
  • Win10+Ollama+AnythingLLM+DeepSeek构建本地多人访问知识库
  • 大数据示例:改变业务的 6 种方式
  • 【虚幻引擎UE】AOI算法介绍与实现案例
  • 【C++八股】std::atomic —— 原子操作
  • ASP.NET Core 如何使用 C# 向端点发出 POST 请求
  • openAI官方prompt技巧(二)
  • 基于springboot+vue的文物管理系统的设计与实现
  • android手机安装deepseek-r1:1.5b