亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解決學生標記儀表板的各種問題 - 我們不能在python中使用簡單的代碼來解決這個問題嗎..?

如何解決學生標記儀表板的各種問題 - 我們不能在python中使用簡單的代碼來解決這個問題嗎..?

心有法竹 2022-08-16 10:41:20
考慮兩個列表中給出的班級學生的分數列表Students = ['student1','student2','student3','student4','student5','student6','student7','student8','student9','student10']Marks = [45, 78, 12, 14, 48, 43, 45, 98, 35, 80]從上面兩個列表中,學生[0]得到分數[0],學生[1]得到分數[1]等等 誰得到的分數介于>25百分位<75百分位之間,按分數的遞增順序排列我的問題 - 我們不能在python中使用簡單的代碼來解決這個問題嗎??我已經寫了代碼,直到這里。要查找數字>25 和 <75,但無法按升序排列。Sort() 不起作用,排序也不工作。請幫助如何提取特定的數組值并分配給另一個數組來解決此問題。for i in range(0,10):    if Marks[i]>25 and Marks[i]<75:         print(Students[i],Marks[i])        print(i)
查看完整描述

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])


查看完整回答
反對 回復 2022-08-16
?
達令說

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}

總結一下:首先將每個學生與它的分數聯系起來,然后進行過濾和排序。我將結果存儲為字典,因為它似乎更方便。


查看完整回答
反對 回復 2022-08-16
?
大話西游666

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


查看完整回答
反對 回復 2022-08-16
  • 3 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號