如何用字符串和数字对列表进行排序
嗨,我是Python的新手,想知道我如何按价格从最大到最小的价格进行排序
Item1 = input("What item do you want to buy? ") # asking what item is
Cost1 = float(input("How much does this item cost? ")) # asking cost of item
Item2 = input("What item do you want to buy? ")
Cost2 = float(input("How much does this item cost? "))
Item3 = input("What item do you want to buy? ")
Cost3 = float(input("How much does this item cost? "))
Item4 = input("What item do you want to buy? ")
Cost4 = float(input("How much does this item cost? "))
Item5 = input("What item do you want to buy? ")
Cost5 = float(input("How much does this item cost? "))
List = [(Item1, Cost1), (Item2, Cost2), (Item3, Cost3), (Item4, Cost4), (Item5, Cost5)] # making a list of other lists
#bubble sort function so it prints in the correct order
def bubble_sort(List):
# Outer loop for traverse the entire list
for i in range(0,len(List)-1):
for j in range(len(List)-1):
if(List[j]<List[j+1]):
temp = List[j]
List[j] = List[j+1]
List[j+1] = temp
return List
print("Your shopping list is: ",bubble_sort(List))
hi im new to python and was wondering how i can sort by the price going from biggest to smallest but im not sure how to do that without just switching the order of the items and cost in the list
Item1 = input("What item do you want to buy? ") # asking what item is
Cost1 = float(input("How much does this item cost? ")) # asking cost of item
Item2 = input("What item do you want to buy? ")
Cost2 = float(input("How much does this item cost? "))
Item3 = input("What item do you want to buy? ")
Cost3 = float(input("How much does this item cost? "))
Item4 = input("What item do you want to buy? ")
Cost4 = float(input("How much does this item cost? "))
Item5 = input("What item do you want to buy? ")
Cost5 = float(input("How much does this item cost? "))
List = [(Item1, Cost1), (Item2, Cost2), (Item3, Cost3), (Item4, Cost4), (Item5, Cost5)] # making a list of other lists
#bubble sort function so it prints in the correct order
def bubble_sort(List):
# Outer loop for traverse the entire list
for i in range(0,len(List)-1):
for j in range(len(List)-1):
if(List[j]<List[j+1]):
temp = List[j]
List[j] = List[j+1]
List[j+1] = temp
return List
print("Your shopping list is: ",bubble_sort(List))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以使用索引访问元组t =(项目,成本)的各个元素,以便t [0]将t和t [1]中的项目引用到t中的成本。因此,您可以通过列表[J] [1]&lt; list [J+1] [1]替换条件列表[J]&lt; list [J+1],以比较其第二个组件;请参阅下面的代码。
The individual elements of a tuple t=(item, cost) can be accessed using indexing, so that t[0] refers to the item in t and t[1] to the cost in t. Thus, you can replace the condition List[j]<List[j+1] by List[j][1]<List[j+1][1], in order to compare their second components; see code below.
提供的示例最简单的解决方案,
一旦您拥有所有输入,只需按价格排序,这就是全部
reverse = true
会排序下降,因为您想要从最大到最小。The easiest solution for provided example,
once you have all inputs, just sort by price and that's all
reverse=True
will sort descending, since you want from biggest to smallest.