Python 从嵌套列表打印出最高值项
代码需要打印出为商店赚最多钱的狗的品种。我知道我需要将数量项目[2]和金钱项目[3]相乘,以确定每只狗首先在商店赚了多少钱,但我不知道如何让系统打印出最畅销的狗。它必须在def函数中解决,有人知道我可以做什么来解决这个问题吗,谢谢Python 从嵌套列表打印出最高值项,python,printing,calculator,nested-lists,python-3.9,Python,Printing,Calculator,Nested Lists,Python 3.9,代码需要打印出为商店赚最多钱的狗的品种。我知道我需要将数量项目[2]和金钱项目[3]相乘,以确定每只狗首先在商店赚了多少钱,但我不知道如何让系统打印出最畅销的狗。它必须在def函数中解决,有人知道我可以做什么来解决这个问题吗,谢谢 # List of transactions [[breed, Dogname, quantity bought, price, age of dog]] lst_dogs = [ ["Pitbull", "Rover"
# List of transactions [[breed, Dogname, quantity bought, price, age of dog]]
lst_dogs = [
["Pitbull", "Rover", 2, 90, 5],
["Greyhound", "Jess", 1, 100, 6],
["Lab", "Rose", 1,85, 3],
["Pitbull", "Daisy", 1, 90, 3],
]
def best_dog():
sum = 0
for i in range(len(lst_dogs)):
item = lst_dogs[i]
for item in lst_dogs:
amount = item[2]
price = item[3]
total = amount * price
sum += total
print("The dog which has earned the most money for the shop is", item[0])
期望输出:为商店赚最多钱的狗是:斗牛犬
实际输出:进程结束,退出代码为0
lst_狗=[
[Pitbull,Rover,2,90,5],
[灰狗,杰斯,1100,6],
[实验室,罗斯,1,85,3],
[Pitbull,Daisy,1,90,3],
]
best_dog=maxlst_dogs,key=lambda项目:项目[2]*项目[3]
打印为商店赚最多钱的狗是best_dog[0]
将max功能与一个键一起使用:
lst_狗=[
[Pitbull,Rover,2,90,5],
[灰狗,杰斯,1100,6],
[实验室,罗斯,1,85,3],
[Pitbull,Daisy,1,90,3],
]
best_dog=maxlst_dogs,key=lambda项目:项目[2]*项目[3]
打印为商店赚最多钱的狗是best_dog[0]
拿这个@cgl345
def givebest(lst_dogs):
best_dog = max(lst_dogs, key=lambda item: item[2] * item[3])
print(f'Best dog is {best_dog[0]}')
拿这个@cgl345
def givebest(lst_dogs):
best_dog = max(lst_dogs, key=lambda item: item[2] * item[3])
print(f'Best dog is {best_dog[0]}')
def givebestlst_dogs:best_dog=maxlst_dogs,key=lambda item:item[2]*item[3]printf'best dog是{best_dog[0]}'def givebestlst_dogs:best_dog=maxlst_dogs,key=lambda item:item[2]*item[3];printf'Best dog is{Best_dog[0]}定义givebestlst_dogs:Best_dog=maxlst_dogs,key=lambda项目:项目[2]*项目[3]printf'Best dog is{Best_dog[0]}定义givebestlst_dogs:Best_dog=maxlst_dogs,key=lambda项目:项目[2]*项目[3];printf'Best dog是{Best_dog[0]}'