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.
import numpy as np data_type = [('name', 'S15'), ('class', int), ('height', float)] students_details = [('James', 5, 48.5), ('Nail', 6, 52.5),('Paul', 5, 42.10), ('Pit', 5, 40.11)] # create a structured array students = np.array(students_details, dtype=data_type) print("Original array:") print(students) print("Sort by height") print(np.sort(students, order='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 height [(b'Pit', 5, 40.11) (b'Paul', 5, 42.1 ) (b'James', 5, 48.5 ) (b'Nail', 6, 52.5 )]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer