array from list of lists

Tim Hochberg tim.hochberg at ieee.org
Sun Nov 12 23:01:26 EST 2006


Erin Sheldon wrote:
> On 11/12/06, Tim Hochberg <tim.hochberg at ieee.org> wrote:
>   
>> I haven't been following this too closely, but if you need to transpose
>> your data without converting all to one type, I can think of a couple of
>> different approaches:
>>
>>     1.  zip(*yourlist)
>>     2. numpy.transpose(numpy.array(yourlist, dtype=object)
>>
>> I haven't tested them though (particularly the second one), so caveat
>> emptor, etc, etc.
>>     
>
> Its not that I want to transpose data.
>
> I'm trying to convert the output of a pgdb postgres query into
> an array with fields and types corresponding to the columns
> I have selected.  The problem is pgdb does not return a list
> of tuples as it should according to DB 2.0, but instead
> a list of lists.   So numpy.array(lol, dtype=) fails, and so will your
> solution #2. 
In that case, I suggest just using a list comprehension or map, 
[tuple(x) for x in lol] for example.

>   I don't want to copy the data more than once
> obviously, so I'm looking for a way to call array() with a lists
> of lists.
>   
It's probably pointless to worry about his. You are already allocating 
5*N python objects (all those Python floats and integers as well as the 
lists themselves). I believe the list comprehension above is only going 
to allocate an additional N objects (the new tuples). Admittedly, the 
objects aren't all the same size, but in this case they are close enough 
that I doubt it'll matter.

-tim




-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list