Python中的索引

2024-06-16 12:24:55 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我这里有一段代码,我正在为一所学校工作

def sem1Sort(semester1, selectionSEM1):
    for semester1["1"] in semester1:
        if semester1["1"] in selectionSEM1:
             print semester1["1"]

def main():     
    selectionSEM1 = ["a", "t", "b", "f", "d", "e"]

    semester1 = {"1": ['a', 'e', 't', 'x', 'l', 'y'], "2": ['b', 'f', 'h', 'm', 'r', 'd'] ,
    "3": ['a', 'b', 'j', 'k', 'o', 'q', 'u'], "4": ['c', 'l', 't', 'z', 'd', 'f'],
    "5": [], "6": [], "7": [], "8": []}


main()

因此,在sem1Sort():方法中,它应该获取semest1列表,以及人工选择的sem1列表。之后,对于学期[“1”]列表中的每个不同索引,如果它在selectionSEM1中,它应该打印出来,对吗?你知道吗


Tags: 方法代码in列表forifmaindef
1条回答
网友
1楼 · 发布于 2024-06-16 12:24:55

我想你的问题是“我怎样才能得到学生选课和一个学期的课程?”。在不改变数据格式的情况下,尝试此筛选方法。你知道吗

def sem1Sort(semester1, selectionSEM1):
    for period in semester1:
        if period == '1':
            for cls in semester1[period]:
                if cls in selectionSEM1:
                     print cls

因为你只检查了第一节课

def sem1Sort(semester1, selectionSEM1):
    print '\n'.join([cls for cls in semester1['1'] if cls in selectionSEM1])

相关问题 更多 >