【问题标题】:Finding Multiple Max Values查找多个最大值
【发布时间】:2019-03-10 04:09:49
【问题描述】:

我正在解决以下问题:

一家超市想要奖励每天最好的顾客,在超市的屏幕上显示顾客的名字。为此,客户的购买金额存储在一个列表中,客户的姓名存储在另一个列表中。实现一个函数 nameOfBestCustomer(sales, customers),它返回销售额最大的客户的姓名。编写一个程序,提示收银员输入所有价格和名称,将它们添加到两个列表中,调用您实现的方法并显示结果。使用 0 的价格作为标记。

我的以下代码工作正常,只是它没有考虑到多个客户的最大购买金额相同。关于如何轻松解决此问题的任何建议?我显然是 Python 新手,因此也欢迎您提出任何其他建议!谢谢!

sales = []
customers = []

def customerSales() :
        salesEntry = 0.01
        customersEntry = 0

        while salesEntry > 0 :
                salesEntry = float(input("Enter new purchase amount or a 0 to finish: "))
                if salesEntry > 0 :
                        sales.append(salesEntry)
                        customersEntry = input("Enter customer name: ")
                        customers.append (customersEntry)
customerSales()


def nameOfBestCustomer(sales, customers) :
#@param: sales and customers lists
#@return: none

        bestCustomer = ""
        salesMax = 0

        salesMax = max(sales)
        index = sales.index(salesMax)
        bestCustomer = customers[index]
        print("The best customer of the day was " + bestCustomer + ".")
        print("They spent $%.2f" % salesMax + ".")

nameOfBestCustomer(sales, customers)

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    现在,您正在使用index 来获得第一个销售额最高的客户。听起来您希望获得与最大销售额相关的所有客户。有几种方法可以做到这一点,但列表理解尤其是 Pythonic:

    indices = [sale for sale in sales if sale == salesMax]
    

    然后,要返回客户,您可以使用另一个列表推导:

    bestCustomers = set(customers[index] for index in indices)
    

    您可以阅读有关列表推导的更多信息here。请注意,第二个示例不是“列表”,但它使用相同的理解语法来创建生成器。我将其输入 python set 以确保它返回不同的客户。

    其他注意事项

    • 您无需提前设置bestCustomersalesMax。它们可以即时创建。
    • 如果您使用我的代码,则需要更改打印答案的方式。
    • 我想知道您正在处理的问题是否真的希望您按客户计算销售额并找到支出最高的人。在这种情况下,您可能会发现 zipgroupBy 非常方便。

    【讨论】:

      【解决方案2】:

      找到 maxSale 后,您可以开始一个循环来查找所有最大销售额和客户。您还需要列出 bestCustomers 列表。希望这会有所帮助。

      def nameOfBestCustomer(sales, customers) :
          #@param: sales and customers lists
          #@return: none
      
          bestCustomers = []
          salesMax = 0
      
          salesMax = max(sales)
          for i, sale in enumerate(sales):
              if sale == salesMax:
                  bestCustomers.append(customers[i])
      
          print("The best customer of the day was", end='')
          for customer in bestCustomers:
              print(' ' + customer, end='')
          print('.')
      

      【讨论】:

        【解决方案3】:

        如果您的结果有多个相同的最大值,您可以将 nameOfBestCustomer 函数更改为这个:

        def nameOfBestCustomer(sales, customers) :
        #@param: sales and customers lists
        #@return: none
        
            bestCustomer = ""
            salesMax = 0
        
            salesMax = max(sales)
            for i in range(len(customers)):
                if sales[i] == salesMax:
                    print("The best customer of the day was " + customers[i] + ".")
            print("They spent $%.2f" % salesMax + ".")
        

        【讨论】:

        • 谢谢!我试图这样想,但我一直对范围(len())感到困惑。我想我还有更多的学习要做!
        • @Katie len(customers) 返回客户列表的长度,range 函数生成数字序列。例如range(3) 返回[0, 1, 2]range(10) 返回[0,1,2,3,4,5,6,7,8,9]。然后range(len(customers)) 返回一个列表,其中包含从零到customers 长度的所有数字
        猜你喜欢
        • 2022-09-29
        • 2020-07-29
        • 2018-09-11
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 2020-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多