Optimization of groupby in python3
<p>I'm currently using following code:</p>

<pre><code>r = sorted(r, key=lambda x: np.sum(np.sign(x[0])))
r_sign = [list(g) for k, g in groupby(r, lambda x: np.sum(np.sign(x[0])))]
</code></pre>

<p>and this repeats dozen of times. This lambda calculates sum of signs in first subarray in array x.</p>

<p>After using cProfile I found out that it takes fifth of all program time to execute this lambdas.
Is there any way to make this faster?</p>

<p>EDIT:
Sample r:</p>

<pre><code>[
[[0.00951770648888711 , -0.010632367736364043 ,-0.00762821730429671 ], [43.41121518753604 , 42.95209626953438 , 42.62569485872547 ]],
[[-0.006837011753845527 ,-0.0635460919772306 , -0.00000010000000494736474], [42.70701121975766 , 40.077577445066126 , 40.07757343730839 ]],
[[-0.00805079210543345 ,0.00013611978285009367 , -0.00928941925049471 ],[42.65520573526393 , 42.66101234779449 ,42.26655131202322 ]],
[[0.0053552539095161516 ,0.034029663355880035 ,0.0040874730381727006 ], [43.23089361326043 , 44.72734381081924 ,44.91053977192976 ]],
[[-0.00995255607863394 ,0.003448983498802789 , -0.008569474771224724 ], [42.574162688526165 , 42.721253784685985 ,42.35671924378283 ]],
[[0.007829733915254721 ,-0.029076385744620153 , 0.01793276260136461 ], [43.33800005686172 ,42.096031132792795 , 42.85773861262677 ]],
[[0.030049593754386345 ,0.02425545805866335 , 0.0007543244036476204 ], [44.3117424911824 ,45.399685036745204 ,45.43394404666549 ]],
[[-0.031584760068658485 ,-0.024818728517988022 , -0.00604796913646777 ], [41.66307961235872 ,40.64178103974877 , 40.3967226022796 ]],
[[-0.024428341523493616 ,0.012410082068531779 , -0.010577084021758344 ],[41.96230747078123 , 42.48630786915531 ,42.03929483570234 ]],
[[-0.023662332321561542 ,0.004586640946688178 , 0.001141053576375452 ], [41.99446329869585 ,42.18751922425499 , 42.2356849185371 ]],
]
</code></pre>