0

可能重复:
在没有内置函数的情况下按降序排列文件中的元素

这是我到目前为止所写的:

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']

我想要的是在我输出文件中的数字后,按降序组织它们。我已经在这几天了。我理解将最低点返回到最后一行的概念。但是我该怎么做呢?我很困。

4

0 回答 0