牛魔王的故事
对代码进行少量添加即可解决此问题,以下是解决方案Students = ['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]Students,Marks=zip(*sorted(zip(Students, Marks))) #addition to your codefor i in range(0,10): if Marks[i]>25 and Marks[i]<75: print(Students[i],Marks[i])
达令说
看起来@Green斗篷人的答案是正确的。但无论如何,如果你想要的是获取两个范围之间有分数的学生的数据,我会这样做:# Get a dict of students with it's mark, filtered by those with mark between 25 and 75students_mark = {s: m for s, m in zip(Students, Marks) if m > 25 and m < 75}# Sort resultsres = dict(sorted(students_mark.items(), key=lambda i: i[1])# res: {'student9': 35, 'student6': 43, 'student1': 45, 'student7': 45, 'student5': 48}# In one lineres = {s: m for s, m in sorted(zip(Students, Marks), key=lambda i: i[1]) if m > 25 and m < 75}总结一下:首先将每个学生与它的分数联系起来,然后进行过滤和排序。我将结果存储为字典,因为它似乎更方便。
大话西游666
第25百分位是“拿走东西的人中倒数第四”,第75百分位是“前四”,无论实际得分如何。因此,您需要做的是对列表进行排序,然后根据索引从中间取出一个切片。以下是我认为您正在尝试执行的操作:import mathstudents = ['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]# zip() will bind together corresponding elements of students and marks# e.g. [('student1', 45), ('student2', 78), ...]grades = list(zip(students, marks))# once that's all in one list of 2-tuples, sort it by calling .sort() or using sorted()# give it a "key", which specifies what criteria it should sort on# in this case, it should sort on the mark, so the second element (index 1) of the tuplegrades.sort(key=lambda e:e[1])# [('student3', 12), ('student4', 14), ('student9', 35), ('student6', 43), ('student1', 45), ('student7', 45), ('student5', 48), ('student2', 78), ('student10', 80), ('student8', 98)]# now, just slice out the 25th and 75th percentile based on the length of that listtwentyfifth = math.ceil(len(grades) / 4)seventyfifth = math.floor(3 * len(grades) / 4)middle = grades[twentyfifth : seventyfifth]print(middle)# [('student6', 43), ('student1', 45), ('student7', 45), ('student5', 48)]你这里有10名学生,所以你如何舍入取决于你(我选择通过舍入“向内”来严格包括那些严格在25-75百分位内的学生 - 你可以通过切换和来做相反的事情,并让你的最终列表在这种情况下再有两个元素 - 或者你可以以相同的方式四舍五入它们)。twentyfifthseventyfifthceilfloor