f = open(document) #this will open the selected class data
swag = [f.readline(),f.readline(),f.readline(),f.readline(),f.readline(),f.readline()] #need to make go on for amount of line
viewfile = input("Do you wish to view the results?")#This will determine whether or not the user wishes to view the results
if viewfile == 'yes': #If the users input equals yes, the program will continue
order = input("What order do you wish to view to answers? (Alphabetical)") #This will determine whether or not to order the results in alphabetical order
if order == 'Alphabetical' or 'alphabetical':
print(sorted(swag))
if order == 'Top' or 'top':
print(sorted(swag, key=int))
文件内容如下
^{pr2}$我该如何按数字顺序排序,比如降序?在
你必须按数值排序,然后不管你得到什么结果,只要把它颠倒过来就行了。在
这里的关键是通过为
key
参数定义一个要排序的函数,来按您需要做的正确事情进行排序。在这里的函数是lambda,它只返回要排序的字符串的数字部分;它将按升序返回它。在
要颠倒顺序,只需颠倒列表。在
您需要在
:
处拆分每一行。从名字中去掉空格,并将数字转换成整数(或者浮点型,如果有的话)。跳过空行。在排序是直接进行的:
^{pr2}$变化
使用
itemgetter
:相关问题 更多 >
编程相关推荐