在python中,如何使用列表中的值作为对另一个值的引用?
我在向python求教,所以我想知道如何编写一个程序来读取代码、价格和许多产品的数量。 然后,程序应打印成本较高的产品的代码和单位较少的产品的代码。 下面是我试图做的:在python中,如何使用列表中的值作为对另一个值的引用?,python,arrays,list,Python,Arrays,List,我在向python求教,所以我想知道如何编写一个程序来读取代码、价格和许多产品的数量。 然后,程序应打印成本较高的产品的代码和单位较少的产品的代码。 下面是我试图做的: Price=[] code=[] quantity=[] Num=x for i in range(x): code.append(input("product code: ")) price.append(float(input("price: "))) quantity.append(int
Price=[]
code=[]
quantity=[]
Num=x
for i in range(x):
code.append(input("product code: "))
price.append(float(input("price: ")))
quantity.append(int(input("quantity: ")))
print(code[max(price)])
print(code[min(quantity)])
已经谢谢你了 一些可能会给您带来麻烦的项目:
应该是Price=[]
Price=[]
应该是Num=x
或其他数字x=10
下的所有三行都需要有相同的缩进for
print(code[price.index(max(price))])
print(code[quantity.index(min(quantity))])
您可以使用
zip
组合所有项目,并使用max
查找最大项目值
all_items = zip(price, code, quantity)
item_with_max_price = max(all_items, key=lambda x: x[0])
item_with_max_quantity = max(all_items, key=lambda x: x[2])
它做错了什么?您需要使用
argmax
而不是max
argmax
不是python中内置的,但您可以从许多库(如numpy)中获得它。然而,既然你正在学习,我建议你自己去实现它,这是一个很好的练习,并不难。你应该把你的问题重新命名为有用的