w3resource

NumPy: Sort the student id with increasing height of the students from given students id and height

NumPy Sorting and Searching: Exercise-4 with Solution

Write a NumPy program to sort the student id with increasing height of the students from given students id and height. Print the integer indices that describes the sort order by multiple columns and the sorted data.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating NumPy arrays for student IDs and their heights
student_id = np.array([1023, 5202, 6230, 1671, 1682, 5241, 4532])
student_height = np.array([40., 42., 45., 41., 38., 40., 42.0])

# Sorting the indices based on 'student_id' and then 'student_height'
indices = np.lexsort((student_id, student_height))

# Displaying the sorted indices
print("Sorted indices:")
print(indices)

# Displaying the sorted data based on the obtained indices
print("Sorted data:")
for n in indices:
    print(student_id[n], student_height[n]) 

Sample Output:

Sorted indices:
[4 0 5 3 6 1 2]
Sorted data:
1682 38.0
1023 40.0
5241 40.0
1671 41.0
4532 42.0
5202 42.0
6230 45.0

Explanation:

In the above code –

student_id = np.array([1023, 5202, 6230, 1671, 1682, 5241, 4532]): This line creates a Numpy array student_id containing seven student IDs.

student_height = np.array([40., 42., 45., 41., 38., 40., 42.0]): This line creates a Numpy array student_height containing the corresponding heights of the seven students.

indices = np.lexsort((student_id, student_height)): This line uses the np.lexsort function to sort the indices based on two sorting keys: student_height and student_id. The np.lexsort function sorts the indices in ascending order first by the last sorting key (student_height) and then by the second-to-last sorting key (student_id).

The for loop iterates over the sorted indices and prints the corresponding student ID and height values:

Sorted data:

1682 38.0

1023 40.0

5241 40.0

1671 41.0

4532 42.0

5202 42.0

6230 45.0

Note:

numpy.lexsort: Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, lexsort returns an array of integer indices that describes the sort order by multiple columns. The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. The keys argument must be a sequence of objects that can be converted to arrays of the same shape. If a 2D array is provided for the keys argument, its rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.

Pictorial Presentation:

NumPy: Sort the student id with increasing height of the students from given students id and height.

Python-Numpy Code Editor:

Previous: Write a NumPy program to create a structured array from given student name, height, class and their data types. Now sort by class, then height if class are equal.
Next: Write a NumPy program to get the indices of the sorted elements of a given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.