Python 是否根据键的值在字典中显示最大的项?
我有一个字典,我想显示三个最大的项,但我希望它基于键的int值。代码如下:Python 是否根据键的值在字典中显示最大的项?,python,python-3.x,dictionary,key,Python,Python 3.x,Dictionary,Key,我有一个字典,我想显示三个最大的项,但我希望它基于键的int值。代码如下: cust_numb = int(input("Enter number of unique customers then press enter:")) list_customer = {} customers = 1 for y in range(0,cust_numb): purch_numb = int(input("How many purchases did customer %s have % (c
cust_numb = int(input("Enter number of unique customers then press enter:"))
list_customer = {}
customers = 1
for y in range(0,cust_numb):
purch_numb = int(input("How many purchases did customer %s have %
(customers)))
list_customer.update({"customer{}".format(customers) : purch_numb})
customers = customers + 1
info = ""
for customer in range(0,cust_numb):
info += "%s " % ("customer {}".format(customer+1)) + "had %s " %
(list_customer["customer{}".format(customer+1)]) + "purchases. "
info = dict(dictionary)
我在网上找不到这个,非常感谢您的帮助 没有简单的方法,因为在这种情况下通常不使用字典。它需要一些列表理解,或者其他一些解决方法。下面是一种方法,它形成一个排序的值列表,然后获取与这些排序的值列表对应的键
d = {'a':2,'b':1,'c':5,'d':0}
sList = sorted(d.values())
op = []
for i in sList:
op.append([k for k,v in d.items() if v == i][0])
print (op)
不清楚你想要什么。请显示示例输入和想要的输出。假设字典是Customer 1:4、Customer 2:6、Customer 3:1、Customer 4:9、Customer 5:2——我希望它返回Customer 1、Customer 2、Customer 4,那么您想返回字典中三个最大值的键吗?(没有特定的顺序?)最好返回输入的顺序(代码的第五行)。代码有什么问题?我们需要更多的信息来回答。