ctypes pointer into the middle of a numpy array

2024/9/27 20:08:01

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)

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.

Any hint appreciated.

Answer

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)
https://en.xdnf.cn/q/71329.html

Related Q&A

Extracting unsigned char from array of numpy.uint8

I have code to extract a numeric value from a python sequence, and it works well in most cases, but not for a numpy array.When I try to extract an unsigned char, I do the followingunsigned char val = b…

How to hold keys down with pynput?

Im using pynput and I would like to be able to hold keys down, specifically wasd but when I try and run this code it only presses the key and doesnt hold it for 2 seconds. If anyone knows what Im doing…

How well does your language support unicode in practice?

Im looking into new languages, kind of craving for one where I no longer need to worry about charset problems amongst inordinate amounts of other niggles I have with PHP for a new project.I tend to fin…

analogy to scipy.interpolate.griddata?

I want to interpolate a given 3D point cloud:I had a look at scipy.interpolate.griddata and the result is exactly what I need, but as I understand, I need to input "griddata" which means some…

Mongodb TTL expires documents early

I am trying insert a document into a Mongo database and have it automatically expire itself after a predetermine time. So far, my document get inserted but always get deleted from the database from 0 -…

sqlalchemy, hybrid property case statement

This is the query Im trying to produce through sqlalchemySELECT "order".id AS "id", "order".created_at AS "created_at", "order".updated_at AS "u…

Whats the newest way to develop gnome panel applets (using python)

Today Ive switched to GNOME (from XFCE) and found some of the cool stuff missing and I would like to (try to) do them on my own. I tried to find information on how to develop Gnome applets (items you p…

How to install opencv-python in python 3.8

Im having problem during the installation of opencv-python in pycharm. After opening pycharm I click on settings and then project interpreter, I click on + and search for the correct module, I started …

python augmented assignment for boolean operators

Does Python have augmented assignment statements corresponding to its boolean operators?For example I can write this:x = x + 1or this:x += 1Is there something I can write in place of this:x = x and yT…

Change a pandas DataFrame column value based on another column value

I have a dataframe with two columns each of which represents an organism. They are called ORG1 and ORG2 I want to move the values of ORG2 into ORG1 for the corresponding index value.So, if ORG1 is A a…