How can I add certain elements in this 2d data structure and calculate a mean - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How can I add certain elements in this 2d data structure and calculate a mean (/thread-37318.html) |
How can I add certain elements in this 2d data structure and calculate a mean - TheOddCircle - May-26-2022 # ------------------------------------------------------------ # Import libraries # ------------------------------------------------------------ # =====> Write your code here mean = 0 index = 0 # ------------------------------------------------------------ # Constants # ------------------------------------------------------------ # =====> Write your code here Pass = (mean >= 50) Fail = (mean <= 49) # ------------------------------------------------------------ # Global variables # ------------------------------------------------------------ myClass = [["Lang", "Carla", 49, 71, 95, 50], ["Bucklund", "Pia", 86, 78, 83, 64], ["Lang", "Jason", 95, 57, 92, 62], ["Dimitrousis", "Hector", 93, 45, 89, 96], ["Owens", "Sunna", 45, 50, 46, 54], ["Goldin", "Sandra", 25, 60, 45, 55], ["Giles", "Seth", 67, 73, 93, 64], ["Gurillo", "Melanie", 88, 88, 62, 79], ["Rykiel", "Kari", 67, 92, 54, 86], ["Shailes", "Dennis", 56, 70, 84, 62]] # =====> Write your code here for mean in myClass: for total in myClass[index:] total = myClass[2] + myClass[3] + myClass[4] + myClass[5] index += 1 print (total)This was my attempt at doing it by indexing the first row and moving down by adding one each time to the 2d data structure place. I want to be able to calculate the mean of the last four numbers in the data structure. The numbers would be [2][3][4][5] but I do not know how to calculate it each time for each row on the list. I tried using for mean in myClass[index]: but it would give no extra help. Any ideas RE: How can I add certain elements in this 2d data structure and calculate a mean - Gribouillis - May-26-2022 myClass is a list of students, so your loop could look likefor myStudent in myClass: # do something with myStudentEach student's data is a 1D data structure (a Python list here). RE: How can I add certain elements in this 2d data structure and calculate a mean - deanhystad - May-26-2022 You might want to "unpack" the student like this: last_name, first_name, *grades = mystudent RE: How can I add certain elements in this 2d data structure and calculate a mean - paul18fr - May-27-2022 I would have done something like: import numpy as np myClass = [["Lang", "Carla", 49, 71, 95, 50], ["Bucklund", "Pia", 86, 78, 83, 64], ["Lang", "Jason", 95, 57, 92, 62], ["Dimitrousis", "Hector", 93, 45, 89, 96], ["Owens", "Sunna", 45, 50, 46, 54], ["Goldin", "Sandra", 25, 60, 45, 55], ["Giles", "Seth", 67, 73, 93, 64], ["Gurillo", "Melanie", 88, 88, 62, 79], ["Rykiel", "Kari", 67, 92, 54, 86], ["Shailes", "Dennis", 56, 70, 84, 62]] myClass = np.asarray(myClass) # get numbers only + mean calculations Notes = myClass[:, 2:].astype(float) Mean = np.mean(Notes, axis = 1).reshape(-1, 1) # check status (pass by default) Status = np.empty( (10,1), dtype='object') Status[:] = 'Pass' # now we can look for 'failed' status t = np.where(Mean < 50) Status[t] = 'Fail' # Concat results myClass = np.hstack((myClass, Mean, Status)) print(f"{myClass}")Providing: penultimate column = meanLast column = status |