Python Function: Get Scores of Selected Names from a List
Here is an implementation of the get_scores_of_selected_names() function in Python:
from typing import List
from collections import namedtuple
StudentScore = namedtuple('StudentScore', ['name', 'score'])
def get_scores_of_selected_names(selection: List[str], scores: List[StudentScore]) -> List[StudentScore]:
'''
Given a list of student names and a list of student scores, returns a list of
namedtuples containing the names and scores of the selected students.
'''
selected_scores = []
for student_score in scores:
if student_score.name in selection:
selected_scores.append(student_score)
return selected_scores
# Example usage:
scores = [
StudentScore('li', 30),
StudentScore('hu', 23),
StudentScore('huai', 99)
]
selection = ['li', 'huai']
for student_score in get_scores_of_selected_names(selection, scores):
print('{} - {}'.format(student_score.name, student_score.score))
This function takes a list of student names (selection) and a list of StudentScore namedtuples (scores) as input. It iterates through the scores list, checking if each student's name is present in the selection list. If a match is found, the corresponding StudentScore namedtuple is added to the selected_scores list, which is returned as the final output.
The example usage demonstrates how to use the get_scores_of_selected_names() function with a sample dataset. It prints the names and scores of the selected students, showing the output of the function.
原文地址: https://www.cveoy.top/t/topic/lPpo 著作权归作者所有。请勿转载和采集!