Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a np array that is constructed as the intersection of 2 other arrays in the following way:

The first array is:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ...,
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

The second array is:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ...,
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

There are in fact multiple differences between the two arrays, but they occur mainly in the middle rows.

The code used to construct the intersection is:

def multidim_intersect(arr1, arr2):
    arr1_view = arr1.view([('',arr1.dtype)]*arr1.shape[1])
    arr2_view = arr2.view([('',arr2.dtype)]*arr2.shape[1])
    intersected = np.intersect1d(arr1_view, arr2_view)
    return intersected.view(arr1.dtype).reshape(-1, arr1.shape[1])

The outputted array is:

[['!' '!']
 ['!' '! !']
 ['!' '! ! !']
 ...,
 ['}' 'was']
 ['}' 'was postponed']
 ['}' '{of']]

As you can see, my new array is sorted differently from the original two arrays (which have multiple exclamation marks sorted before single exclamation marks, as would be done in LC_ALL=C sort). Is there any way to sort my outputted array like my other arrays? Note that the shape of the array is important.

@Mr E arr1 and arr2 were originally lists. I can't give you the exact copy, but I will do my best to construct an example which illustrates what I need.

arr1 = [['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

arr2 = [['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

Ideally, the output would be:

[['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['!' '!']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

but instead it is:

[['!' '!']
 ['! ! !' '! ! ! !']
 ['! ! !' '! ! ! "']
 ['! ! !' '! ! ! .']
 ['}' 'was postponed']
 ['}' 'was']
 ['}' '{of']]

or something to that effect.

share|improve this question
1  
Can you give Python code for arr1 and arr2 that can be copied and pasted into a terminal or editor? –  Mr E Nov 13 '14 at 14:36
    
Sure just something representative and ideally the output you expect –  Mr E Nov 13 '14 at 14:40
    
I edited my question. –  Bib Nov 13 '14 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.