Create 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.
# Python 3.9.5
import numpy as np
def enter_data_st(num):
list_st = []
for i in range(1, num+1):
name = input(f'Enter name student {i}: ')
rank = int(input(f'Enter rank student {i}: '))
height = float(input(f'Enter height student {i}: '))
tup_st = name, rank, height,
list_st.append(tup_st)
return list_st
def sort_st(list_st):
data_type = [('name', 'S15'), ('class', int), ('height', float)]
students = np.array(list_st, dtype=data_type)
result = np.sort(students, order=['class', 'height'])
return result
def main():
num = int(input('Enter number students: '))
list_st = enter_data_st(num)
print('Original array: ', list_st)
print('Array sorted by order: ', sort_st(list_st))
if __name__ == '__main__':
main()
Comments
Leave a comment