in c extension what is easiest way to build a (PyObject) list from an array of doubles?

  • Thread starter Christian Seberino
  • Start date
C

Christian Seberino

In c extension what is easiest way to build a (PyObject) list from an
array of doubles?

I don't think I can do "return Py_BuildValue(...) to make a list from
an array can I???

How else can I build and return a list??

Thanks!

Chris+
 
P

Paul Prescod

Christian said:
In c extension what is easiest way to build a (PyObject) list from an
array of doubles?

No. Check out the array module.

Paul Prescod
 
S

seberino

Paul

Thanks. I agree array is great in Python for processing
homogenous lists but what about in **C code** if you must
return a Python list built from a C array?

How do you build a list/tuple/array/ from a C array of numbers?

Chris

No. Check out the array module.

Paul Prescod

--
_______________________________________

Christian Seberino, Ph.D.
SPAWAR Systems Center San Diego
Code 2872
49258 Mills Street, Room 158
San Diego, CA 92152-5385
U.S.A.

Phone: (619) 553-9973
Fax : (619) 553-6521
Email: (e-mail address removed)
_______________________________________
 
A

Andrew MacIntyre

How do you build a list/tuple/array/ from a C array of numbers?

The API docs are what you need to look at.

To create a tuple, call PyTuple_New().

For each element in your C array, build a Python object then use
PyTuple_SetItem() to insert it into the tuple.
 
S

seberino

Andrew

Thanks. I did it and it looks like it works too.

Chris

The API docs are what you need to look at.

To create a tuple, call PyTuple_New().

For each element in your C array, build a Python object then use
PyTuple_SetItem() to insert it into the tuple.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: (e-mail address removed) (pref) | Snail: PO Box 370
(e-mail address removed) (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

--
_______________________________________

Christian Seberino, Ph.D.
SPAWAR Systems Center San Diego
Code 2872
49258 Mills Street, Room 158
San Diego, CA 92152-5385
U.S.A.

Phone: (619) 553-9973
Fax : (619) 553-6521
Email: (e-mail address removed)
_______________________________________
 
S

Skip Montanaro

Chris> In c extension what is easiest way to build a (PyObject) list
Chris> from an array of doubles?

Chris> I don't think I can do "return Py_BuildValue(...) to make a list
Chris> from an array can I???

If the length of the array is known when you write the code I think
something like this will work:

return Py_BuildValue("[dddd]", a[0], a[1], a[2], a[3]);

Chris> How else can I build and return a list??

Something like this (untested, no error checking):

int alen = sizeof(a)/sizeof(a[0]);
PyObject *list_of_floats = PyList_New(alen);
for (i = 0; i < alen; i++) {
PyList_SET_ITEM(list_of_floats, i, PyInt_FromLong(a));
}
return list_of_floats;

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top