views:

72

answers:

2

Say I have a numpy matrix like so:

[[ x1, x2, x3, ... ],
 [ y1, y2, y3, ... ],
 [ z1, z2, z3, ... ],
 [ 1,  1,  1,  ... ]]

From which I want to extract a list of lists like so:

[[x1, y1, z1], [x2, y2, z2], [x3, y3, z3], ... ]

What is the most optimal way of doing this?

At the moment I have:

tpoints = [pt[:3].tolist() for pt in numpy.asarray(tptmat.T)]

And the call to tolist() is taking up disproportionate amount of time, approximately a third of the time spent in the most time consuming function of my program.

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 14422540   69.777    0.000   69.777    0.000 {method 'tolist' of 'numpy.ndarray' objects}
       20   64.258    3.213  178.057    8.903 trans.py:152(_apply)
      ...
+1  A: 

Have you tried zip(*matrix)? This will leave you with

[[x1, y1, z1, 1], [x2, y2, z2, 1], [x3, y3, z3, 1], ... ]

But list generation will probably still happen...

Wait (slaps palm on forehead)! This should do the trick:

zip(*matrix[:3])

In the interactive shell:

>>> matrix = [[ 11, 12, 13, 14],
...           [ 21, 22, 23, 24],
...           [ 31, 32, 33, 34],
...           [  1,  1,  1,  1]]
>>> zip(*matrix[:3])
[(11, 21, 31), (12, 22, 32), (13, 23, 33), (14, 24, 34)]
>>>

This is a list of tuples, though, but does that really matter?

Daren Thomas
No, a list of tuples wouldn't have mattered at all. As long as it's a sequence of a sequence.And +1 for making me realise I could use zip on a numpy matrix! Ah, the wonders of duck typing :)
falcon
+3  A: 
KennyTM
+1 for being smarter!
Daren Thomas
Perfect! And it looks even better as well. I had a feeling I was over complicating things.
falcon
Also, yes `tolist()` is necessary in this case unfortunately. Code which I have no control over chokes if I pass it a numpy array.
falcon