[英]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.