0

I am working on a Python C-extension code. Currently, a 1D C-array is converted to a Python List. Now I need to convert an ND-array, described by a 1) data type, 2) shape (as a 1D integer vector, for example, 2x4x3 array), and 3) data binary payload as serialized array elements of data-type.

I noticed that there is an array object in python: https://docs.python.org/2/library/array.html

I prefer to convert the ND array buffer to an array object instead of requiring installation of numpy ndarray - an additional dependency, but I failed to find any C-API to create/read/write Python-array objects from C.

does this interface (something similar to PyList_New) exist?

6
  • 1
    The Python array class is not multi-dimensional. But for interface the class offers a buffer protocol. Also switch to Py3 if you can. If you don't want to use numpy, why the [numpy] tag? Commented Jun 17, 2020 at 4:15
  • I found the buffer object API, which supports shapes (ND) and data types: docs.python.org/2/c-api/… is a buffer object good for this purpose? Commented Jun 17, 2020 at 4:20
  • 1
    I haven't used it so can't help. BUT, what do you intend to do with these 'arrays' in Python. Without numpy or PIL it doesn't look like you can do much; at least I'm not aware of any builtin packages that use attributes like shape and strides. Commented Jun 17, 2020 at 4:28
  • 1
    You should almost certainly use either nested lists or NumPy. array.array arrays will be useless to you, and the buffer API is mostly useful for C-level access, not for providing a Python interface. Commented Jun 17, 2020 at 4:44
  • is there a convenient/short way to create multi-dimensional list? the data loading may have various dimensions , 3D, or 4D, a nested loop is a bit cumbersome? alternatively, a list reshape function in C-API would also solve this problem. this is in C. Commented Jun 17, 2020 at 13:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.