简体   繁体   English

我如何添加另一个简单的 function 以添加两个选项以按数字升序和降序对此代码进行排序?

[英]how do i add another simple function to add two more options to sort this code numerically ascending and descending?

Thank you for everyone who has helped.感谢所有帮助过的人。 i have made the code work thanks to one comment pointing out my mistake, i would now like to add a function for 2 more options to sort this code numerically ascending and descending由于一条评论指出了我的错误,我已经使代码正常工作,我现在想添加一个 function 以获得另外 2 个选项,以按数字升序和降序对这段代码进行排序

fruit= [["apples", 5], ["pears", 10], ["oranges", 6], ["bananas", 12], ["grapes", 100]]

items= [["cars",3], ["sheets",5], ["bananas",8], ["bridges",17], ["roads",0], ["doors",1]]

colours= [["blue",1], ["black",7], ["orange",13], ["yellow",2], ["red",8], ["blue",5]]


merge = fruit + items + colours
result = set(
    (item, sum(quantity for label, quantity in merge if label == item))
    for item, _ in merge
)

print ()        
print ("SHOW DATA")
print ()

print ("fruit :", fruit)
print ("items :", items)
print ("colours :", colours)

print ()
print ("WHICH ORDER?")
print ()

print ("1 = Alphabetically Ascending")
print ("2 = Alphabetically Descending")
print ("")
SortOrder = int(input ("Enter the sort order you prefer for the merged data >"))

if (SortOrder == 1):
    lists = sorted(result)
    for l in lists:
       print(l)

elif (SortOrder == 2):
    lists = sorted(result, reverse=True)
    for l in lists:
       print(l)

A set does not have the .sort() method, you need to use the sorted() method, and you are looping through lists which in not defined.集合没有.sort()方法,您需要使用sorted()方法,并且循环遍历未定义的lists

Here is a solution:这是一个解决方案:

if (SortOrder == 1):
    lists = sorted(result)
    for l in lists:
       print(l)
elif (SortOrder == 2):
    lists = sorted(result, reverse=True)
    for l in lists:
       print(l)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我想按一个标准升序排序并按另一个标准降序排序,我该如何使用 sort 函数? - How can i use the sort function if i want to sort ascending by a criterion and descending by another criterion? 如何对三个 Vals 的压缩列表进行排序,其中我想要前两个反转(降序)而第三个不是(升序)? - How do I Sort a Zipped List of Three Vals where I want first two Reversed (Descending) and Third is not (Ascending)? 如何按数字(降序)和字母顺序对元组的相同索引排序? [蟒蛇] - How do I sort the same index of a tuple by Numerically (descending) and Alphabetically? [Python] 如何制作我的按钮,以降序方式对用户输入的数字进行排序,因为它只会升序 - How do I make my button, sort the numbers inputted by a user in a descending way, as it only does ascending 如何按升序/降序对 tweepy 在 Python 中返回的解析不佳的 JSON 数据进行排序? - How do I sort in ascending/descending order this poorly parsed JSON data returned by tweepy in Python? Python:如何将文本文件中的整数按升序和/或降序排序? - Python: How do i sort integers in a text file into ascending and/or descending order? 我需要添加什么,让我的Python 3程序检查输入是升序还是降序? - What do I need to add, to ask my Python 3 program to check if the input is in ascending or descending order? 如何按数字降序对字典排序 - How to sort a dictionary by descending order numerically 我如何按 seaborn 的升序/降序对直方图条进行排序? - How i can sort histogram bar in ascending/descending order of seaborn? 如何按升序或降序对 matplotlib 图进行排序? - How can i sort the matplotlib graph in ascending or descending order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM