ctypes指向numpy数组中间的指针 [英] ctypes pointer into the middle of a numpy array

查看:146
本文介绍了ctypes指向numpy数组中间的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何获取指向numpy数组开头的ctypes指针:

I know how to get a ctypes pointer to the beginning of a numpy array:

a = np.arange(10000, dtype=np.double)
p = a.ctypes.data_as(POINTER(c_double))
p.contents
c_double(0.0)

但是,我需要将指针传递给元素100,,而无需复制数组. 必须有一种简单的方法来完成它,但是找不到它.

however, I need to pass the pointer to, let's say, element 100, without copying the array. There must be an easy way to do it but cannot find it.

任何提示表示赞赏.

推荐答案

切片numpy数组将创建视图,而不是副本:

Slicing a numpy array creates a view, not a copy:

>>> a = numpy.arange(10000, dtype=numpy.double)
>>> p = a[100:].ctypes.data_as(ctypes.POINTER(ctypes.c_double))
>>> p.contents
c_double(100.0)
>>> a[100] = 55
>>> p.contents
c_double(55.0)

这篇关于ctypes指向numpy数组中间的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆