我需要打印那些多个列表,但它需要按字母顺序打印,.sort无法工作,因为涉及到数字。
"""define a function to retrive short shelf life items in alphabetical order"""
def retrieveShortShelfLifeItems(oneItemList):
if shelfLife <= 7:
shortLifeItemsList.append(oneItemList)
return shortLifeItemsList
#initializes short shelf life list
shortLifeItemsList = []
shortLifeItems = [['Steak', ' 10.00', ' 7', '10.50'], ['Canned Corn', ' .50', ' 5', '0.53']]
#print items with short shelf life
for item in shortLifeItems:
print("{:^20s}${:^16s}{:^20s}${:^16s}"\
.format((item[0]),item[1],item[2],item[3]))所以它印了:
Steak $ 10.00 7 $ 10.50
Canned Corn $ .50 5 $ 0.53 当它要打印时:
Canned Corn $ .50 5 $ 0.53
Steak $ 10.00 7 $ 10.50 有什么建议吗?
发布于 2014-04-07 02:52:58
发布于 2014-04-07 02:58:09
您需要显式地对列表进行排序。以下是其中的诀窍:
shortLifeItems = sorted(shortLifeItems, key=lambda list: list[0])https://stackoverflow.com/questions/22902825
复制相似问题