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
91 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?

question from:https://stackoverflow.com/questions/65670795/how-to-show-the-initial-array-after-concatenating-and-sorting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

2.1m questions

2.1m answers

60 comments

57.0k users

...