Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

python - how to show the initial array after concatenating and sorting

I have 3 arrays of

a = np.array([[1], [4], [5], [11], [10]])
b = np.array([[14], [3], [2], [13], [12]])
c = np.array([[6], [23], [24], [8], [9]])

I have merged these arrays and sorted them as folow:

new = np.sort(np.concatenate([a, b, c], axis=1)) which results in:

[[ 1  6 14]
 [ 3  4 23]
 [ 2  5 24]
 [ 8 11 13]
 [ 9 10 12]]

Now I am looking for a way to show from what initial array (a,b,c), each value is picked. for example, I want to get

[[ 'a' , 'c' , 'b']
 [ 'b' , 'a' , 'c']
 [ 'b' , 'a' , 'c']
 [ 'c' , 'a' , 'b']
 [ 'c' , 'a' , 'b']]

I am not sure if I am in the right way or should I use dictionaries for this purpose?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use numpy.argsort on the concatenated array:

combined = np.concatenate([a, b, c], axis=1)
np.argsort(combined)

#[[0 2 1]
# [1 0 2]
# [1 0 2]
# [2 0 1]
# [2 0 1]]

vars = np.array(['a','b','c'])
vars[np.argsort(combined)]

#[['a' 'c' 'b']
# ['b' 'a' 'c']
# ['b' 'a' 'c']
# ['c' 'a' 'b']
# ['c' 'a' 'b']]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...