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

Discussion in 'Python' started by Christian Seberino, Feb 3, 2004.

  1. 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+
     
    Christian Seberino, Feb 3, 2004
    #1
    1. Advertising

  2. Christian Seberino

    Paul Prescod Guest

    Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?

    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
     
    Paul Prescod, Feb 3, 2004
    #2
    1. Advertising

  3. Christian Seberino

    Guest

    Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?

    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:
    _______________________________________
     
    , Feb 3, 2004
    #3
  4. Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?

    On Tue, 3 Feb 2004 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: (pref) | Snail: PO Box 370
    (alt) | Belconnen ACT 2616
    Web: http://www.andymac.org/ | Australia
     
    Andrew MacIntyre, Feb 4, 2004
    #4
  5. Christian Seberino

    Guest

    Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?

    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 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: (pref) | Snail: PO Box 370
    > (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:
    _______________________________________
     
    , Feb 5, 2004
    #5
  6. Re: in c extension what is easiest way to build a (PyObject) listfrom an array of doubles?

    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
     
    Skip Montanaro, Feb 10, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dan
    Replies:
    1
    Views:
    2,342
    Jack Klein
    Nov 26, 2003
  2. Olivier Sessink
    Replies:
    0
    Views:
    394
    Olivier Sessink
    Feb 24, 2005
  3. Replies:
    11
    Views:
    490
    David Bolen
    Jul 19, 2006
  4. Allen
    Replies:
    1
    Views:
    343
    Gabriel Genellina
    Jun 12, 2007
  5. MD
    Replies:
    1
    Views:
    265
    Marc 'BlackJack' Rintsch
    Aug 11, 2007
Loading...

Share This Page