【发布时间】:2016-03-07 23:55:24
【问题描述】:
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))
文件读作
John : 1
Ben : 2
Josh : 3
我将如何将它们按数字顺序排列,例如降序?
【问题讨论】:
-
if x == y or z不会像您认为的那样做。另外,如果文件有不同的行数怎么办?另外,int("John : 1")的结果是什么? -
那么我该如何解决这个问题?
-
[f.readline(),f.readline(),..]是一种将文件读入数组的糟糕方法。使用循环,卢克,使用循环! -
您的代码有多个问题。我建议看看official Python tutorial。
-
@tobias_k 使用现有的 API,Luke 将是下一课... :)
标签: python sorting numerical alphanumeric