如何用字符串和数字对列表进行排序

发布于 2025-01-27 13:40:59 字数 1177 浏览 2 评论 0原文

嗨,我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朱染 2025-02-03 13:40:59

可以使用索引访问元组t =(项目,成本)的各个元素,以便t [0]将t和t [1]中的项目引用到t中的成本。因此,您可以通过列表[J] [1]&lt; list [J+1] [1]替换条件列表[J]&lt; list [J+1],以比较其第二个组件;请参阅下面的代码。

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][1]<List[j+1][1]):  
                temp = List[j]  
                List[j] = List[j+1]  
                List[j+1] = temp  
    return List  
  
print("Your shopping list is: ",bubble_sort(List))  

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.

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][1]<List[j+1][1]):  
                temp = List[j]  
                List[j] = List[j+1]  
                List[j+1] = temp  
    return List  
  
print("Your shopping list is: ",bubble_sort(List))  
彡翼 2025-02-03 13:40:59

提供的示例最简单的解决方案,
一旦您拥有所有输入,只需按价格排序,这就是全部

sorted_by_ = sorted(data, key=lambda tup: tup[1], reverse=True)

reverse = true会排序下降,因为您想要从最大到最小。

键函数(docs)

The easiest solution for provided example,
once you have all inputs, just sort by price and that's all

sorted_by_ = sorted(data, key=lambda tup: tup[1], reverse=True)

reverse=True will sort descending, since you want from biggest to smallest.

Key functions (docs)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文