1

I have an array P = np.array([2,3,1]) and I want to assign three strings to each element respectively. So,:

"str1", "str2", "str3" = P

and after sorting the array:

In[]: P = -np.sort(-P)
Out[]: [3,2,1]

I want to then be able to display the strings based on this sort, as:

Out[]: "str2","str1","str3",

Tried assigning variable names to the elements but it won't display on output as intended. Tried defining an array of objects with the strings as elements but have trouble assigning them to the numerical values of P.

1
  • There is no such thing as "assigning strings", if you are trying to create dynamic variables know that this is very bad practice (and often useless). What are you really trying to achieve? Commented Jan 5, 2023 at 17:43

1 Answer 1

1

You can use numpy.argsort.

import numpy as np
P = np.array([2,3,1])
S = np.array(["str1", "str2", "str3"])

sort_idx = np.argsort(-P)
print(S[sort_idx])
# ['str2' 'str1' 'str3']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.