Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找多个最大值_Python_Python 3.x - Fatal编程技术网

Python 查找多个最大值

Python 查找多个最大值,python,python-3.x,Python,Python 3.x,我正在处理以下问题: 超市想奖励每天最好的顾客,在超市的屏幕上显示顾客的名字。为此,客户的购买金额存储在一个列表中,客户的姓名存储在另一个列表中。实现函数nameOfBestCustomer(sales,customers),返回销售额最大的客户的名称。编写一个程序,提示出纳输入所有价格和名称,将它们添加到两个列表中,调用您实现的方法,并显示结果。使用0的价格作为哨兵 我有以下代码正常工作,除了它没有考虑到多个客户的相同购买金额是最大的。有什么建议,我可以很容易地解决这个问题?我显然是Pytho

我正在处理以下问题:

超市想奖励每天最好的顾客,在超市的屏幕上显示顾客的名字。为此,客户的购买金额存储在一个列表中,客户的姓名存储在另一个列表中。实现函数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)

找到maxSale后,可以开始循环以查找所有最大销售额和客户。此外,你还需要列出最佳客户。希望这有帮助

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('.')

如果结果有多个相同的最大值,则可以将
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 + ".")

现在,您正在使用
索引
获取第一个销售额最高的客户。听起来你想让所有的客户都能达成最大销售额。有几种方法可以做到这一点,但列表理解尤其是python:

indices = [sale for sale in sales if sale == salesMax]
然后,要返回客户,您可以使用另一个列表:

bestCustomers = set(customers[index] for index in indices)
你可以阅读更多关于列表理解的内容。注意,第二个示例不是“列表”,但它使用相同的理解语法来创建生成器。我将其输入python
集合
,以确保它返回不同的客户

其他考虑
  • 您不需要提前设置
    bestCustomer
    salesMax
    。它们可以动态创建
  • 如果使用我的代码,您需要更改打印答案的方式
  • 我想知道你正在研究的问题是否真的希望你按客户对销售额进行合计,并找到最花钱的人。在这种情况下,您可能会发现它非常方便

谢谢!我试着这样想,但我一直对range(len())感到困惑。我想我还有更多的学习要做@Katie
len(客户)
返回客户列表的长度,并且
range
函数生成数字序列。例如
范围(3)
返回
[0,1,2]
范围(10)
返回
[0,1,2,3,4,5,6,7,8,9]
。然后
范围(len(customers))
列表包含从零到
customers