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

A

Alex van der Spek

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
 
P

Philip Semanchuk

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
 
R

Robert Kern

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
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top