How to extract arrays from an arranged numpy array?
<p>This is a relative question of the post <a href="http://stackoverflow.com/questions/39673377/how-to-extract-rows-from-an-numpy-array-based-on-the-content/39674145?noredirect=1#comment66837793_39674145">How to extract rows from an numpy array based on the content?</a>, and I used the following code to split rows based on the content in the column:</p>

<pre><code>np.split(sorted_a,np.unique(sorted_a[:,1],return_index=True)[1][1:])
</code></pre>

<p>the code worked fine, but later I tried the code to split other cases (as below), I found that there could be wrong results (as showed in CASE#1).</p>

<pre><code>CASE#1
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
OUTPUT#1
[]
[[2748309, 246211, 1],
 [2748309, 246211, 2],
 [2747481, 246201, 54]]
the result I want
[[2748309, 246211, 1],
 [2748309, 246211, 2]]
[[2747481, 246201, 54]]
</code></pre>

<p>I think the code may successfully split rows only in the case with little numbers, which with less digits, and I don't know how to solve problems showed in CASE#1 above. So in this post, I have 2 little relative questions:</p>

<p><strong>1. How to split rows with greater numbers in it? (as showed in CASE #1)?</strong></p>

<p><strong>2. How to handle (split) data with both cases including #1 rows with the same element in the second column, but different in the first, and #2 rows with the same element in the first column, but different in the second ? (That is, could python distinguish rows considering contents in both first and second columns simultaneously?)</strong></p>

<p>Feel free to give me suggestions, thank you.</p>

<p><strong>Update#1</strong></p>

<p>The <code>ravel_multi_index</code> function could handle this kind of task with integer-arrays, but how to deal with arrays containing float?</p>

<p>Here's an approach considering pair of elements from each row as indexing tuples -</p>

<pre><code># Convert to linear index equivalents
lidx = np.ravel_multi_index(arr[:,:2].T,arr[:,:2].max(0)+1)

# Get sorted indices of lidx. Using those get shifting indices.
# Split along sorted input array along axis=0 using those.
sidx = lidx.argsort()
out = np.split(arr[sidx],np.unique(lidx[sidx],return_index=1)[1][1:])
</code></pre>

<p>Sample run -</p>

<pre><code>In [34]: arr
Out[34]: 
array([[2, 7, 5],
       [3, 4, 6],
       [2, 3, 5],
       [2, 7, 7],
       [4, 4, 7],
       [3, 4, 6],
       [2, 8, 5]])

In [35]: out
Out[35]: 
[array([[2, 3, 5]]), array([[2, 7, 5],
        [2, 7, 7]]), array([[2, 8, 5]]), array([[3, 4, 6],
        [3, 4, 6]]), array([[4, 4, 7]])]
</code></pre>

<p>For a detailed info on converting group of elements as indexing tuple, please refer to <a href="http://stackoverflow.com/a/38674038/3293881"><code>this post</code></a>.</p>

python arrays numpy