w3resource

NumPy: From given student name, height, and class sort by class, then height if class are equal

NumPy Sorting and Searching: Exercise-3 with Solution

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.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Defining the data types for the structured array
data_type = [('name', 'S15'), ('class', int), ('height', float)]

# Defining the details of students as a list of tuples
students_details = [('James', 5, 48.5), ('Nail', 6, 52.5), ('Paul', 5, 42.10), ('Pit', 5, 40.11)]

# Creating a structured array 'students' using the defined data type and provided details
students = np.array(students_details, dtype=data_type)

# Displaying the original structured array
print("Original array:")
print(students)

# Sorting the structured array by 'class' and 'height' fields
print("Sort by class, then height if class are equal:")
print(np.sort(students, order=['class', 'height'])) 

Sample Output:

Original array:
[(b'James', 5, 48.5 ) (b'Nail', 6, 52.5 ) (b'Paul', 5, 42.1 )
 (b'Pit', 5, 40.11)]
Sort by class, then height if class are equal:
[(b'Pit', 5, 40.11) (b'Paul', 5, 42.1 ) (b'James', 5, 48.5 )
 (b'Nail', 6, 52.5 )]

Explanation:

In the above code -

data_type = [('name', 'S15'), ('class', int), ('height', float)]: This line defines a custom data type data_type with three fields: 'name' (a string with a maximum length of 15 characters), 'class' (an integer), and 'height' (a float).

students_details = [('James', 5, 48.5), ('Nail', 6, 52.5),('Paul', 5, 42.10), ('Pit', 5, 40.11)]: This line creates a list of tuples, where each tuple contains student details (name, class, and height).

students = np.array(students_details, dtype=data_type): This line creates a structured Numpy array of students using the custom data type data_type. It assigns students_details elements to the corresponding fields in the array.

print(np.sort(students, order=['class', 'height'])): This line sorts the students array based on two fields: 'class' and 'height'. The sorting is done first by the 'class' field in ascending order. For records with the same 'class' value, sorting is done by the 'height' field in ascending order. The sorted array is then printed.

Pictorial Presentation:

NumPy: From given student name, height, and class sort by class, then height if class are equal.

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 the array on height.
Next: 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.

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.