當我嘗試在Python排序我的名單,我得到以下錯誤: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)
目前打印這樣的:
名稱計量價格單價
looooool 1.0 10.0 10.0
looooool 1.0 10.0 10.0
哈哈1.0 1.0 1.0
過程完成退出代碼0
什麼是你的數據結構是什麼樣子?目前還不清楚。 –
可以請您發佈產品清單嗎? –
你爲什麼對待:money_sum,mes_sum,name_sum等 - 就好像它們是產品一樣? – alfasin