0

我按照冒泡排序重新执行了程序。

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']。现在可能出了什么问题?我做对了输出文件吗?

4

2 回答 2

3

你有这条线的地方:

x = minimum

我想你的意思是:

minimum = x

好像你刚刚得到的分配顺序不正确。x在迭代期间分配给变量A不会有任何副作用。

编辑

正如我在评论中发现的那样,您的问题是您正在使用该readlines()功能,但文件中只有一行。您真正想要做的是阅读该行,然后用于split()生成一个列表:

A = file.read().split()

请记住,由于在与“<”进行比较时使用的是字符串,因此在代码运行后您将不会获得数字顺序,您将获得字典顺序。

例子:

输入:

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之前int对split 返回的每个元素调用 cast 函数。在此更改之后,这是我从您的程序中看到的输出:

输入:

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]
于 2012-12-15T16:28:12.563 回答
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()
于 2012-12-19T17:12:38.000 回答