返回Cython的阵列

发布于 2025-01-26 18:11:34 字数 665 浏览 1 评论 0原文

我需要为大型缓冲区分配内存,而不实际对其进行初始化(结果以后将用作缓冲区,并且每个位置都将写入,因此初始化只是浪费时间)。

我可以在Cython中轻松地使用:

cdef char *buff = <char *> malloc(n * sizeof(char))
if not buff :
        raise MemoryError()

问题是我不确定如何将其作为Ndarray返回到Python(类似于Numpy Array)。该文档显示了如何将其转换为列表并返回列表,但不是作为array.https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html

return [x for x in my_array[:number]]

返回列表并不是很有用,因为我希望将其解释为布尔数组(此缓冲区将用作过滤元素的掩码)。

# Index and coefficient array are both numpy ND-arrays
index_array = index_array[mask]
coeff_array = coeff_array[mask]

计算口罩后,如何将缓冲区作为布尔数组返回?

I need to allocate memory for a large buffer, without actually initializing it (it will be used as a result buffer later and every location will be written to, so initialization is just a waste of time).

I can easily do this in Cython using:

cdef char *buff = <char *> malloc(n * sizeof(char))
if not buff :
        raise MemoryError()

The problem is I'm not sure how to return this as an ndarray to python (similar to numpy array). The documentation shows how to convert this to a list and return it, but not as an array.https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation.html

return [x for x in my_array[:number]]

Returning a list isn't very useful as I want this to be interpreted as a boolean array (this buffer will be used as a mask to filter out elements).

# Index and coefficient array are both numpy ND-arrays
index_array = index_array[mask]
coeff_array = coeff_array[mask]

how can I return the buffer as a boolean array after I calculated a mask?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文