当我尝试用python对列表排序时,出现以下错误:
Traceback (most recent call last):
File "C:/Users/natep/OneDrive/Documents/I.T/Level 2/programing/Shop_list.py", line 81, in <module>
products.sort(key = lambda x:x[3], reverse=True)
Name Measurement Price Unit Price
File "C:/Users/natep/OneDrive/Documents/I.T/Level 2/programing/Shop_list.py", line 81, in <lambda>
products.sort(key = lambda x:x[3], reverse=True)
Chips Drink
IndexError: list index out of range
50 2.0
5.0 3.0
2.5 1.0
Chips Drink
50 2.0
5.0 3.0
2.5 1.0
Process finished with exit code 1
下面是我正在使用的代码。我想知道如何对包含列表的列表进行排序。我还想知道为什么我的数据要打印两次,以及如何更好地格式化打印输出,使其与正确的列相对应。(不导入下载)
任何帮助都将不胜感激。你知道吗
unit_price = price / measurement
unit.append(unit_price)
products.append(name_sum)
products.append(mes_sum)
products.append(money_sum)
products.append(unit)
row_entry.append(products)
average = "The average product price is ${}".format(mean(money_sum))
highest = "The most expensive item is ${}".format(max(money_sum))
low = "The cheapest item is ${}".format(min(money_sum))
print()
print("Name\t\tMeasurement\t\tPrice\t\tUnit Price" )
for item in products:
print("")
for data in item:
print(data, "\t", end='')
products.sort(key = lambda x:x[3], reverse=True)
我在下面添加了所有代码:
# Shopping list
from statistics import mean
from operator import itemgetter
def limit_check(type, question, low, high):
valid = False
error = "please enter values between {} and or equal to {}".format(low,
high)
while not valid:
try:
response = type(input(question))
if low < response <= high:
valid = True
return response
else:
print(error)
except ValueError:
print(error)
products = []
mes_sum = []
money_sum = []
name_sum = []
unit = []
row_entry = []
money = float(input("please enter your budget: $"))
get_data = True
while get_data == True:
name = input("please enter product name: ")
if name.lower() == "xxx":
break
else:
name_sum.append(name)
price = limit_check(float,"How much does the item cost", 0, money)
money_sum.append(price)
measurement = limit_check(int,"Please enter unit of measurement Push 1
for kg, push 2 for g, push 3 for L, push 4 for mL",0,4)
if measurement == int("1"):
kg = limit_check(float, "Enter Kg: ", 0, 10)
mes_sum.append(kg)
elif measurement == int("2"):
grams = limit_check(int, "Enter Grams: ", 0, 999)
mes_sum.append(grams)
elif measurement == int("3"):
litres = limit_check(float, "Enter Litres: ", 0, 30)
mes_sum.append(litres)
elif measurement == int("4"):
mL = limit_check(int, "Enter mL: ", 0, 999)
mes_sum.append(mL)
unit_price = price / measurement
unit.append(unit_price)
for i in range(len(name_sum)):
products.append([name_sum[i], mes_sum[i], money_sum[i], unit[i]])
average = "The average product price is ${}".format(mean(money_sum))
highest = "The most expensive item is ${}".format(max(money_sum))
low = "The cheapest item is ${}".format(min(money_sum))
print()
print("Name\t\tMeasurement\t\tPrice\t\tUnit Price" )
for item in products:
print("")
for data in item:
print(data, "\t", end='')
products.sort(key = lambda x:x[1], reverse=True)
当前打印如下:
名称计量价格单价
Loooool 1.0 10.0 10.0
Loooool 1.0 10.0 10.0
lol 1.0 1.0 1.0
进程已完成,退出代码为0
您的代码似乎期望
products
列表中的每个列表都有4个元素(用于名称、度量、价格和单价),但实际上您的数据在每个列表中只有两个元素(用于薯片和饮料)。你知道吗然后,当您尝试对包含长度为2的列表的
products
排序时,您将访问lambda
函数中的第4个值。这会触发错误:只有两个值,而不是4。你知道吗看起来您已经转置了想要在
products
中结束的数据。而不是:执行:
或者更紧凑地说:
相关问题 更多 >
编程相关推荐