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

Philip Semanchuk philip at semanchuk.com
Sun Sep 18 19:57:10 CEST 2011


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




More information about the Python-list mailing list