如何找到元组中每个标记从0到总标记的频率

时间:2019-06-18 16:57:58

标签: python list loops tuples

根据基于班级学生在基于项目的评估中所获得的分数,老师正在生成少量报告。 假设她的10个学生的分数在一个元组中可用。满分为25。

编写一个python程序来实现以下功能: find_more_than_average():查找并返回得分高于全班平均成绩的学生百分比

sort_marks():按从0到25的升序对标记进行排序。排序后的值应填充在列表中并返回

generate_frequency():查找有多少学生得分相同。例如,有多少人得分为0,有多少人得分为1,有多少人得分为3…。有多少人得分为25。结果应填充在列表中并返回。

我得到的平均数和排序部分正确。但是对于频率,如果元素重复两次,则频率为1

list_of_marks=(12,18,25,24,2,5,18,20,20,21)

def find_more_than_average():
    sumi=0
    count=0
    sumi=sum(list_of_marks)
    avg=sumi/len(list_of_marks)
    for i in list_of_marks:
        if(i>avg):
            count=count+1
    morethanavg=(count/len(list_of_marks))*100
    return morethanavg


    #Remove pass and write your logic here

def sort_marks():
    return sorted(list_of_marks)
    #Remove pass and write your logic here

def generate_frequency():
    #Remove pass and write your logic here
    gener=[]
    for i in range(0,26):
        if i in list_of_marks:
            gener.append(1)
        else:
            gener.append(0)
    return gener


print(find_more_than_average())
print(generate_frequency())
print(sort_marks())

expected- [0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,2,0,2,1, 0,0,1,1] 实际-[0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,1,0,0 ,1,1]

1 个答案:

答案 0 :(得分:0)

当您这样做:

#!/usr/bin/env python

import wx

#---------------------------------------------------------------------------

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1)
        panel = wx.Panel(self)
        self.SetWindowStyle(wx.STAY_ON_TOP)
        button = wx.Button(panel, -1, "Show MessageDialog", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton)
        self.Show()

    def OnButton(self, event):
        dlg = wx.MessageDialog(self, 'Hello from wxPython!',
                               'A Message Box',
                               wx.YES | wx.NO | wx.ICON_INFORMATION | wx.STAY_ON_TOP
                               )
        dlg.ShowModal()
        dlg.Destroy()


if __name__ == "__main__":
    app = wx.App(False)
    MyFrame(None)
    app.MainLoop()

很明显,您永远不会获得0或1以外的其他值。但是,您希望这些值的计数不仅是1,表明该值在列表中。一种选择是先创建一个零列表,然后逐步遍历标记并将一个添加到与标记相对应的索引中:

if i in list_of_marks:
   gener.append(1)
else:
   gener.append(0)

现在,当您看到20次两次时,您的def generate_frequency(): gener = [0] * 26 for m in list_of_marks: gener[m] += 1 return gener 将会增加两次,结果是:

generator[20]