Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > in c extension what is easiest way to build a (PyObject) list from an array of doubles?

Reply
Thread Tools

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

 
 
Christian Seberino
Guest
Posts: n/a
 
      02-03-2004
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+
 
Reply With Quote
 
 
 
 
Paul Prescod
Guest
Posts: n/a
 
      02-03-2004
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



 
Reply With Quote
 
 
 
 
seberino@spawar.navy.mil
Guest
Posts: n/a
 
      02-03-2004
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
_______________________________________

 
Reply With Quote
 
Andrew MacIntyre
Guest
Posts: n/a
 
      02-04-2004
On Tue, 3 Feb 2004 (E-Mail Removed) 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: (E-Mail Removed) (pref) | Snail: PO Box 370
(E-Mail Removed) (alt) | Belconnen ACT 2616
Web: http://www.andymac.org/ | Australia

 
Reply With Quote
 
seberino@spawar.navy.mil
Guest
Posts: n/a
 
      02-05-2004
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 (E-Mail Removed) 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: (E-Mail Removed) (pref) | Snail: PO Box 370
> (E-Mail 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 Removed)
_______________________________________

 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      02-10-2004

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Easiest way to check if one array is a subset of another Thriving K. Ruby 2 08-04-2009 05:23 AM
easiest way to split a list into evenly divisble smaller lists, and assign them to variables? flamesrock Python 3 06-07-2005 02:17 AM
The easiest way to build a simple photo website Renato Digital Photography 17 04-15-2005 07:11 AM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM
Easiest way to *add a column* to a 2d matrix/array in numarray??? Christian Seberino Python 4 04-27-2004 08:08 PM



Advertisments