Numpy.array with dtype works on list of tuples not on list of lists?

Discussion in 'Python' started by Alex van der Spek, Sep 18, 2011.

  1. Why does this not work?

    >>> dat=[[1,2,3],[4,5,6]]
    >>> col=[('a','f4'),('b','f4'),('c','f4')]
    >>> arr=numpy.array(dat,dtype=col)


    Traceback (most recent call last):
    File "<pyshell#91>", line 1, in <module>
    arr=numpy.array(dat,dtype=col)
    TypeError: expected a readable buffer object
    >>>


    But this does:

    >>> dat=[(1,2,3),(4,5,6)]
    >>> arr=numpy.array(dat,dtype=col)
    >>> arr

    array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
    dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4')])
    >>>


    The only difference that the object is a list of tuples now?

    Thanks for clarification,
    Alex van der Spek
    Alex van der Spek, Sep 18, 2011
    #1
    1. Advertising

  2. Re: Numpy.array with dtype works on list of tuples not on list oflists?

    On Sep 18, 2011, at 11:55 AM, Alex van der Spek wrote:

    > Why does this not work?
    >
    >>>> dat=[[1,2,3],[4,5,6]]
    >>>> col=[('a','f4'),('b','f4'),('c','f4')]
    >>>> arr=numpy.array(dat,dtype=col)

    >
    > Traceback (most recent call last):
    > File "<pyshell#91>", line 1, in <module>
    > arr=numpy.array(dat,dtype=col)
    > TypeError: expected a readable buffer object
    >
    > But this does:
    >
    >>>> dat=[(1,2,3),(4,5,6)]
    >>>> arr=numpy.array(dat,dtype=col)
    >>>> arr

    > array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4')])
    >
    > The only difference that the object is a list of tuples now?


    I don't know why you're seeing what you're seeing, but if you don't get answer here you could try asking on the numpy list.

    Good luck
    Philip
    Philip Semanchuk, Sep 18, 2011
    #2
    1. Advertising

  3. Alex van der Spek

    Robert Kern Guest

    Re: Numpy.array with dtype works on list of tuples not on list oflists?

    On 9/18/11 10:55 AM, Alex van der Spek wrote:
    > Why does this not work?
    >
    >>>> dat=[[1,2,3],[4,5,6]]
    >>>> col=[('a','f4'),('b','f4'),('c','f4')]
    >>>> arr=numpy.array(dat,dtype=col)

    >
    > Traceback (most recent call last):
    > File "<pyshell#91>", line 1, in <module>
    > arr=numpy.array(dat,dtype=col)
    > TypeError: expected a readable buffer object
    >>>>

    >
    > But this does:
    >
    >>>> dat=[(1,2,3),(4,5,6)]
    >>>> arr=numpy.array(dat,dtype=col)
    >>>> arr

    > array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', '<f4'), ('b', '<f4'),
    > ('c', '<f4')])
    >>>>

    >
    > The only difference that the object is a list of tuples now?


    numpy questions are best asked on the numpy mailing list:

    http://www.scipy.org/Mailing_Lists

    To answer your question, though, numpy.array() needs to figure out a lot of
    different things about the input data simultaneously, in particular its shape.
    Structured arrays (i.e. with elements that have individual fields as above) pose
    a new problem in that its individual elements are sequences themselves. In order
    to help it decide whether it should recurse down into a sequence to find its
    elements or decide that the sequence *is* an element in its own right, we
    settled on the convention that tuples are to be considered elements and that
    lists are sequences of elements.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
    Robert Kern, Sep 18, 2011
    #3
    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. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    391
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  2. Daniel Nogradi
    Replies:
    3
    Views:
    344
    Dennis Lee Bieber
    Nov 10, 2006
  3. Replies:
    5
    Views:
    541
    Thomas J. Gritzan
    Oct 6, 2006
  4. Replies:
    2
    Views:
    479
    Robert Kern
    Nov 13, 2007
  5. Jon Reyes
    Replies:
    18
    Views:
    220
    Mitya Sirenef
    Feb 19, 2013
Loading...

Share This Page