By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

P: n/a
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+
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Christian Seberino wrote:
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

Jul 18 '05 #2

P: n/a
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

On Mon, Feb 02, 2004 at 11:04:13PM -0800, Paul Prescod wrote:
Christian Seberino wrote:
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


--
_______________________________________

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: se******@spawar.navy.mil
_______________________________________

Jul 18 '05 #3

P: n/a
On Tue, 3 Feb 2004 se******@spawar.navy.mil wrote:
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.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

Jul 18 '05 #4

P: n/a
Andrew

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

Chris

On Wed, Feb 04, 2004 at 07:21:15PM +1100, Andrew MacIntyre wrote:
On Tue, 3 Feb 2004 se******@spawar.navy.mil wrote:
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.

--
Andrew I MacIntyre "These thoughts are mine alone..."
E-mail: an*****@bullseye.apana.org.au (pref) | Snail: PO Box 370
an*****@pcug.org.au (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: se******@spawar.navy.mil
_______________________________________

Jul 18 '05 #5

P: n/a

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[i]));
}
return list_of_floats;

Skip

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.