Answer to Question #283352 in Python for ramk

Question #283352

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.


1
Expert's answer
2021-12-28T12:59:41-0500
# 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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS