This code implements the get_scores_of_selected_names(names) function in Python, which retrieves scores for selected student names from a list of StudentScore namedtuples.

from collections import namedtuple

StudentScore = namedtuple('StudentScore', ['name', 'score'])

scores = [
    StudentScore('li', 30),
    StudentScore('hu', 23),
    StudentScore('huai', 99)
]

def get_scores_of_selected_names(names):
    selected_scores = []
    for score in scores:
        if score.name in names:
            selected_scores.append(score)
    return selected_scores

The function takes a list of student names as input and iterates through the scores list. It checks if each StudentScore's name is present in the provided list. If so, the corresponding StudentScore is added to the selected_scores list, which is then returned.

Here's an example of how to use the function:

selection = ['li', 'huai']
for student_score in get_scores_of_selected_names(selection):
    print('{} - {}'.format(student_score.name, student_score.score))

This code snippet outputs the following:

li - 30
huai - 99

This function is useful for scenarios where you need to retrieve scores for specific students from a larger dataset.

Python Function to Get Scores of Selected Students

原文地址: https://www.cveoy.top/t/topic/lPpj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录