← Back to NumPy Course | Chapter 8: Statistics | Lesson 6 of 6

np.sort/argsort का इस्तेमाल

sort values order करता है और argsort बताता है वे positions जो इन्हें sort करेंगी।

In this page:

  1. np.sort/argsort
Syntax
python
sorted_arr = np.sort(arr)
indices = np.argsort(arr)
arr.sort()    # in place

np.sort/argsort

np.sort एक sorted copy return करता है जबकि arr.sort() in place sort करता है। argsort indices return करता है जो array sort करेंगी, आपको एक दूसरे array को match करने के लिए reorder करने देते हुए। 2-D data पर, axis चुनता है किस direction में sort करना है।

Note: descending order के लिए argsort()[::-1] इस्तेमाल करें।

उदाहरण: np.sort/argsort

python
import numpy as np

names = np.array(["Ann", "Bob", "Cy"])
scores = np.array([88, 95, 70])
order = np.argsort(scores)[::-1]
print(np.sort(scores))
print(order)
print(names[order])

# Output:
# [70 88 95]
# [1 0 2]
# ['Bob' 'Ann' 'Cy']
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि arr.sort() None return करता है
  2. जब आपको indices चाहिए sort इस्तेमाल करना
  3. गलत axis sort करना
चैप्टर सारांश
  • np.sort एक copy return करता है
  • arr.sort in place sort करता है
  • argsort indices return करता है
  • [::-1] से reverse करें
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.