可能重复:
Putting elements from file in descending order without built in functions
这是我到目前为止所写的:
def main():
try:
file=open(input("Please enter the name of the file you wish to open:" ))
A =file.read().split()
n=len(A)
print ("These following", n,"numbers are in the inputted file:\n", A)
x=(int, A)
new_list=[]
while A:
minimum=A[0]
for i in range(n):
for j in range(n-1):
if A[0]<A[1]:
minimum=A[0]
new_list.append(A[0])
print ("Now, lets organize these numbers from highest to lowest:", new_list)
file.close()
except IOError as e:
print("({})".format(e)
main()
这是我跑完后得到的:
Please enter the name of the file you wish to open:numbers.txt (<<<file I created)
These following 9 numbers are in the inputted file:
['1', '-3', '10', '6', '5', '0', '3', '-5', '20']
我想要的是在输出文件中的数字后,按降序排列它们。我已经好几天了。我理解将最低点返回到线的后面的概念。但是我该怎么做呢?我很困惑。