在没有内置函数的情况下按降序放置文件中的元素

时间:2012-12-15 16:25:15

标签: python file list sorting

我在泡泡排序后重新完成了该程序。

def main():
try:
    array=[]
    file=open(input("Please enter the name of the file you wish to open:" ))
    A =file.read().split()


    file.close()



    n = len(A)
    print ("These following", n,"numbers are in the inputted file:\n", A)
    for i in range(n):
        for j in range(1,n-i):

            if A[j-1] < A[j]:

                (A[j-1], A[j]) = (A[j],A[j-1])
    print("We can now organize it in descending order:\n", A)

except IOError as e:
    print("({})".format(e))


Output_File = input("Where would you like to save this data?")
fileObject = open(Output_File, 'a')
fileObject.write(str(Output_File)+'\n')
print("Your file is now saved as", Output_File,". \n Have a nice day!")
fileObject.close()

如果名称 =='主要':     main()的

问题是,它对列表中的每3个数字进行排序。所以,如果我有9个数字,它将有3个不同的数字。例如,1 -3 10 6 5 0 3 -5 20,将是['6','5','3','20','10','1','0',' - -5 ',' - 3']。现在可能出现什么问题?我输出文件是否合适?

2 个答案:

答案 0 :(得分:3)

你有这条线:

x = minimum

我认为你的意思是:

minimum = x

好像你刚刚得到的任务订单不正确。在x的迭代期间分配给变量A不会产生任何副作用。

修改

我在评论中发现的问题是您使用的是readlines()函数,但文件中只有一行。你真正想做的是读取该行,然后使用split()生成一个列表:

A = file.read().split()

请记住,因为在与'&lt;'进行比较时使用的是字符串,所以在运行代码后,您将无法获得数字顺序。

示例:

输入:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200

输出:

['-1']
['-1', '-10']
['-1', '-10', '0']
['-1', '-10', '0', '14']
['-1', '-10', '0', '14', '2']
['-1', '-10', '0', '14', '2', '200']
['-1', '-10', '0', '14', '2', '200', '3']
['-1', '-10', '0', '14', '2', '200', '3', '4']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8']
['-1', '-10', '0', '14', '2', '200', '3', '4', '4', '5', '6', '7', '8', '9']

请注意上面200不在最后但是在2之后,为了获得数字顺序,您需要将字符串强制转换为数字数据类型,可能是int。使用map函数读取文件中的数字时,可以轻松完成此操作:

A = map(int, file.read().split())

这将在将元素存储在A中之前调用split 返回的每个元素上的int强制转换函数。在此更改之后,这是我从您的程序中看到的输出:

输入:

5 4 14 6 -1 2 0 9 8 7 3 4 -10 200

输出:

[-10]
[-10, -1]
[-10, -1, 0]
[-10, -1, 0, 2]
[-10, -1, 0, 2, 3]
[-10, -1, 0, 2, 3, 4]
[-10, -1, 0, 2, 3, 4, 4]
[-10, -1, 0, 2, 3, 4, 4, 5]
[-10, -1, 0, 2, 3, 4, 4, 5, 6]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14]
[-10, -1, 0, 2, 3, 4, 4, 5, 6, 7, 8, 9, 14, 200]

答案 1 :(得分:0)

我完成了!我只需要找出一种将列表转换为整数的不同方法。 这是:

def main():
try:
    file=open(input("Please enter the name of the file you wish to open:" ))
    A = []
    #Here I convert the list to integers to separate as numbers in order to sort later
    for val in file.read().split():
        A.append(int(val))
    file.close()
    n = len(A)
    print ("These following", n,"numbers are in the inputted file:\n", A)

    for i in range(n):
        for j in range(1,n-i):
            if A[j-1] < A[j]:
                (A[j-1], A[j]) = (A[j],A[j-1]) #swap
    print("We can now organize it in descending order:\n", A)

    Output_File = input("Where would you like to save this data?")
    fileObject = open(Output_File, 'a')
    fileObject.write(str(Output_File)+'\n')
    print("Your file is now saved as",Output_File,".\nHave a nice day!")
    fileObject.close()

except IOError as e:
    print("({})".format(e))




if __name__ == '__main__':
main()