【问题标题】:Trying to figure out the average of existing objects in a class试图找出类中现有对象的平均值
【发布时间】:2017-11-28 04:43:02
【问题描述】:

我需要帮助来计算每位员工的平均工资。
这是我目前拥有的代码。

class Associate:
    ID = 0;
    avgPay = 0.0

    def __init__(self, ID, name, pay):
        self.ID = ID
        self.name = name
        self.pay = pay
        Associate.ID += 1
        Associate.avgPay = (pay + Associate.avgPay)/Associate.ID

    def speak(self):
        print('Name: ', self.name,'Pay: ', self.pay )

a1 = Associate('A111','Emily',85000)
a1.speak()
print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
a2 = Associate('A222','Bob',88000)
a2.speak()
print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
a3 = Associate('A333','John',92000)
a3.speak()
print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
a4 = Associate('A444','Tom',77000)
a4.speak()
print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))

我的问题是,每次调用 Associate.avgPay 时,它都会将上次调用的平均值相加,而不是添加 self.pay
当前输出:

Name: Emily Pay:  85000
ID = 1, avgPay = 85000.0
Name: Bob Pay:  88000
ID = 2, avgPay = 86500.0
Name: John Pay:  92000
ID = 3, avgPay = 59500.0
Name: Tom Pay:  77000
ID = 4, avgPay = 34125.0

正确的输出:

Name: Emily Pay:  85000
ID = 1, avgPay = 85000.0
Name: Bob Pay:  88000
ID = 2, avgPay = 86500.0
Name: John Pay:  92000
ID = 3, avgPay = 88333.33333
Name: Tom Pay:  77000
ID = 4, avgPay = 85500.0

任何帮助将不胜感激。

【问题讨论】:

  • 平均值不是这样的。您需要一份所有付款的清单

标签: python python-3.x class average


【解决方案1】:

如果您为两个以上的对象编写了在纸上尝试的平均算法,您会发现您实际上并没有计算平均值。

您需要一次将所有值相加,然后除,而不是将新值添加到已除的值中,然后再次除...

此外,您正在重复代码这一事实意味着您确实应该考虑使用列表。也不需要任何类属性

这是一个例子

associates = [] 
associates.append(Associate('A111','Emily',85000))
associates.append(Associate('A222','Bob',88000)) 
associates.append(Associate('A333','John',92000)) 
associates.append(Associate('A444','Tom',77000)) 

pays = [a.pay for a in associates] 
avg = sum(pays)/len(pays) 
for id, a in enumerate(associates):
    a.speak()
    print("ID = {0}, avgPay = {1}".format(id, avg))

【讨论】:

    【解决方案2】:

    至少,平均计算似乎是错误的。 应该是,

    Associate.avgPay = (pay + Associate.avgPay * (Associate.ID-1))/Associate.ID
    

    【讨论】:

    • 你是乘以取消之前的除法吗?
    • 大概,是的。分子必须是过去支付的总和。
    • 看起来还是不对,因为你还需要在乘以之前减去以前的工资
    • 嗯......我很害怕,但你真的尝试过吗?至少,修改后的方程输出给定值的真实平均值。
    【解决方案3】:

    正如@cricket_007 所说,平均值不会那样工作。您可以做的是跟踪列表中的所有付款并使用它来计算平均值:

    class Associate:
        ID = 0;
        payList = []
    
        def __init__(self, ID, name, pay):
            self.ID = ID
            self.name = name
            self.pay = pay
            Associate.ID += 1
            Associate.payList.append(pay)
            Associate.avgPay = sum(Associate.payList) / len(Associate.payList)
    

    【讨论】:

      【解决方案4】:

      如果您不想保留列表,则可以将总工资作为附加类别属性保留:

      class Associate:
          ID = 0;
          avgPay = 0.0
          totalPay = 0.0
      
          def __init__(self, ID, name, pay):
              self.ID = ID
              self.name = name
              self.pay = pay
              Associate.totalPay+=pay
              Associate.ID += 1
              Associate.avgPay = Associate.totalPay / Associate.ID
      
          def speak(self):
              print('Name: ', self.name,'Pay: ', self.pay )
      

      然后您的一系列对象按您的意愿工作:

      a1 = Associate('A111','Emily',85000)
      a1.speak()
      print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
      a2 = Associate('A222','Bob',88000)
      a2.speak()
      print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
      a3 = Associate('A333','John',92000)
      a3.speak()
      print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay))
      a4 = Associate('A444','Tom',77000)
      a4.speak()
      print("ID = {0}, avgPay = {1}".format(Associate.ID,Associate.avgPay)) 
      

      打印:

      Name:  Emily Pay:  85000
      ID = 1, avgPay = 85000.0
      Name:  Bob Pay:  88000
      ID = 2, avgPay = 86500.0
      Name:  John Pay:  92000
      ID = 3, avgPay = 88333.33333333333
      Name:  Tom Pay:  77000
      ID = 4, avgPay = 85500.0
      

      【讨论】:

        猜你喜欢
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2022-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多