Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created November 27, 2019 15:24
Show Gist options
  • Save vlad-bezden/fc6f5b10ea3f3483ac14c260346da5f8 to your computer and use it in GitHub Desktop.
Save vlad-bezden/fc6f5b10ea3f3483ac14c260346da5f8 to your computer and use it in GitHub Desktop.
Finding and Locating Maximum Elements Using numpy
import numpy as np
students = ["Alice", "Bob", "Carl", "David"]
subjects = ["Math", "Physics", "Biology"]
# Quiz scores
# - Columns -> subjects
# - Rows -> students
scores = np.array([[10, 8, 5], [6, 9, 8], [9, 9, 8], [7, 5, 9]])
print("\nSubject and name of the student, and the highest score")
for subject, scores_ in zip(subjects, scores.T):
print((subject, students[np.argmax(scores_)], np.max(scores_)))
# print highest score and subject name for each student
print("\nStudent and name of the subject with the highest score")
for student, scores_ in zip(students, scores):
print((student, subjects[np.argmax(scores_)], np.max(scores_)))
# output
# Subject and name of the student, and the highest score
# ('Math', 'Alice', 10)
# ('Physics', 'Bob', 9)
# ('Biology', 'David', 9)
# Student and name of the subject with the highest score
# ('Alice', 'Math', 10)
# ('Bob', 'Physics', 9)
# ('Carl', 'Math', 9)
# ('David', 'Biology', 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment