Complete the code to get the indices that would sort the array.
import numpy as np arr = np.array([3, 1, 2]) sorted_indices = np.[1](arr) print(sorted_indices)
The np.argsort() function returns the indices that would sort the array.
Complete the code to get the indices that would sort the array in descending order.
import numpy as np arr = np.array([5, 2, 9, 1]) sorted_indices = np.argsort(arr)[[1]] print(sorted_indices)
Using [::-1] reverses the array of indices to get descending order.
Fix the error in the code to correctly get sorted indices of a 2D array along axis 1.
import numpy as np arr = np.array([[3, 1, 2], [9, 7, 8]]) sorted_indices = np.argsort(arr, axis=[1]) print(sorted_indices)
Axis 1 means sorting each row. So axis=1 is correct.
Fill both blanks to create a dictionary with words as keys and their sorted index of length as values, only for words longer than 3 letters.
words = ['apple', 'bat', 'carrot', 'dog'] lengths = {word: len(word) for word in words if len(word) [1] 3} sorted_indices = np.argsort([lengths[word] for word in lengths]) result = {word: sorted_indices[[2]] for [2] in range(len(sorted_indices))} print(result)
We filter words with length greater than 3 using > 3. The loop variable is i but here we reuse {{BLANK_2}} for the index in range.
Fill all three blanks to create a dictionary mapping each word to its sorted index based on length, only for words longer than 3 letters.
words = ['pear', 'kiwi', 'banana', 'fig'] lengths = {word: [1] for word in words if [2](word) > 3} sorted_indices = np.argsort([lengths[word] for word in lengths]) result = {word: sorted_indices[[3]] for [3] in range(len(sorted_indices))} print(result)
We use len(word) to get length, loop over word, and use i as index in the final dictionary comprehension.