Array 1 is called 'students', with 'Alex', 'Rich', 'Anthony', 'Len', 'Mark' as values. Array 2 is called 'grades' with [85, 44], [63, 19], [47, 95], [30, 67], [33, 16] as values.
I need to select all rows from 'grades' where 'students' is either 'Alex' or 'Mark'
Do I need to combine the arrays? I am new to python and struggling to figure out how to index this correctly.
so far I have created the two arrays and have tried concatenating them together, but when I then try to index off the concatenated array I get errors
students = np.array(['Alex', 'Rich', 'Anthony', 'Len', 'Mark'])
grades = np.array([[85, 44], [63, 19], [47, 95], [30, 67], [33, 16]])
studentgrades = np.concatenate((students, grades), axis=1)`
studentgrades['Alex']
CodePudding user response:
students = ['Alex', 'Rich', 'Anthony', 'Len', 'Mark']
grades = [[85, 44], [63, 19], [47, 95], [30, 67], [33, 16]]
studentgrades = dict(zip(students, grades))
print(studentgrades)
{'Alex': [85, 44],'Rich': [63, 19],'Anthony': [47, 95],'Len': [30, 67],'Mark': [33, 16]}
print(studentgrades['Alex'])
[85, 44]
CodePudding user response:
here is one way to do it using list comprehension
assumption: grade and student indexes are aligned
# iterate through the students
# when student is among the list, return their grades
[grades[i] for i in range(len(students)) if students[i] in ['Alex','Mark']]
[array([85, 44]), array([33, 16])]