3 回答

TA貢獻1830條經驗 獲得超3個贊
對代碼進行少量添加即可解決此問題,以下是解決方案
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 code
for i in range(0,10):
if Marks[i]>25 and Marks[i]<75:
print(Students[i],Marks[i])

TA貢獻1821條經驗 獲得超6個贊
看起來@Green斗篷人的答案是正確的。但無論如何,如果你想要的是獲取兩個范圍之間有分數的學生的數據,我會這樣做:
# Get a dict of students with it's mark, filtered by those with mark between 25 and 75
students_mark = {s: m for s, m in zip(Students, Marks) if m > 25 and m < 75}
# Sort results
res = dict(sorted(students_mark.items(), key=lambda i: i[1])
# res: {'student9': 35, 'student6': 43, 'student1': 45, 'student7': 45, 'student5': 48}
# In one line
res = {s: m for s, m in sorted(zip(Students, Marks), key=lambda i: i[1]) if m > 25 and m < 75}
總結一下:首先將每個學生與它的分數聯系起來,然后進行過濾和排序。我將結果存儲為字典,因為它似乎更方便。

TA貢獻1817條經驗 獲得超14個贊
第25百分位是“拿走東西的人中倒數第四”,第75百分位是“前四”,無論實際得分如何。因此,您需要做的是對列表進行排序,然后根據索引從中間取出一個切片。
以下是我認為您正在嘗試執行的操作:
import math
students = ['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 tuple
grades.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 list
twentyfifth = 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
添加回答
舉報