0
0
NumPydata~5 mins

np.sort() for sorting arrays in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does np.sort() do in NumPy?

np.sort() sorts the elements of an array in ascending order and returns a new sorted array without changing the original array.

Click to reveal answer
intermediate
How can you sort a 2D NumPy array by rows using np.sort()?

Use np.sort(array, axis=1) to sort each row independently in ascending order.

Click to reveal answer
beginner
Does np.sort() modify the original array?

No, np.sort() returns a new sorted array and leaves the original array unchanged.

Click to reveal answer
intermediate
What parameter in np.sort() controls the sorting axis?

The axis parameter controls which axis to sort along. For example, axis=0 sorts each column, axis=1 sorts each row.

Click to reveal answer
intermediate
How do you sort a NumPy array in descending order using np.sort()?

Sort the array in ascending order with np.sort() and then reverse it using slicing: np.sort(array)[::-1].

Click to reveal answer
What does np.sort() return?
AA new sorted array
BThe original array sorted in place
CA boolean indicating if the array is sorted
DThe sum of array elements
How do you sort each column of a 2D array using np.sort()?
A<code>np.sort(array, axis=0)</code>
B<code>np.sort(array, axis=1)</code>
C<code>np.sort(array)</code>
D<code>np.sort(array, axis=2)</code>
Which of these sorts an array in descending order using np.sort()?
A<code>np.sort(array, axis=-1)</code>
B<code>np.sort(array, descending=True)</code>
C<code>np.sort(array, reverse=True)</code>
D<code>np.sort(array)[::-1]</code>
If you call np.sort(array) without axis on a 2D array, what happens?
AEach row is sorted independently
BEach column is sorted independently
CThe array is flattened and sorted
DAn error is raised
Does np.sort() change the original array?
AYes, it sorts the original array in place
BNo, it returns a new sorted array
COnly if you set <code>inplace=True</code>
DOnly for 1D arrays
Explain how to use np.sort() to sort a 2D array by rows and by columns.
Think about which axis represents rows and which represents columns.
You got /4 concepts.
    Describe how to sort a NumPy array in descending order using np.sort().
    Consider how to reverse a list or array in Python.
    You got /3 concepts.