I apologise for the title of this question that I know is very unclear, I tried my best.
I have three arrays that need to be sorted, but the tricky rule is the following:
- the first array needs to increment every time, and when the maximum is obtained, goes back to zero.
- the second array has to be sorted starting from the minimum to the maximum.
- The third array is the most complicated one: each position MUST correspond to the doublet of numbers that are in the two firsts arrays. For example, if before sorting, the letter
'L'
inarray3
was at the same position as the doublet(0, 1)
in the two firsts arrays before sorting, it should be the same after sorting.
Because the explanation may be not very clear, here is an example of the starting point:
import numpy as np
array1 = np.array([ 0 , 0 , 1 , 1 , 2 ])
array2 = np.array([ 1 , 0 , 1 , 0 , 0 ])
array3 = np.array(['L', 'H', 'O', 'E', 'L'])
This is the desired output:
array1 = np.array([ 0 , 1 , 2 , 0 , 1 ])
array2 = np.array([ 0 , 0 , 0 , 1 , 1 ])
array3 = np.array(['H', 'E', 'L', 'L', 'O'])
This looks like a very simple problem, but at the moment I don't have found a solution to it.
CodePudding user response:
This is a non-numpy solution. But
The desired order can be obtained by doing
sorted(zip(array2, array1))
To obtain the list of indices (to reorder the word) you could do
indices, sorted_arrays = zip(*sorted(enumerate(zip(array2, array1)), key=itemgetter(1)))
indices is then
(1, 3, 4, 0, 2)
to get the output
"".join(array3[x] for x in indices)
CodePudding user response:
Create a fourth array which is array2 * (array1.max() 1) array1
. Then argsort that as the indices for array3.
import numpy as np
array1 = np.array([ 0 , 0 , 1 , 1 , 2 ])
array2 = np.array([ 1 , 0 , 1 , 0 , 0 ])
array3 = np.array(['L', 'H', 'O', 'E', 'L'])
to_sort = ( array1.max() 1 ) * array2 array1
array3[ np.argsort( to_sort ) ]
# array(['H', 'E', 'L', 'L', 'O'], dtype='<U1')