Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文件中的元素按降序放置,而不使用内置函数_Python_File_List_Sorting - Fatal编程技术网

Python 将文件中的元素按降序放置,而不使用内置函数

Python 将文件中的元素按降序放置,而不使用内置函数,python,file,list,sorting,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 fi

我按照泡泡排序重新做了这个程序

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()
def main():
尝试:
数组=[]
file=open(输入(“请输入要打开的文件名:”)
A=file.read().split()
file.close()文件
n=len(A)
打印(“以下这些”,n,“数字在输入的文件中:\n”,A)
对于范围(n)中的i:
对于范围(1,n-i)内的j:
如果A[j-1]
如果name='main': main()


问题是,它在列表中每3个数字进行一次排序。因此,如果我有9个数字,它将有3个不同的数字。例如,1-3106503-520将是,['6','5','3','20','10','1','0','-5','-3']。现在可能有什么问题?我把输出文件做对了吗

这一行的位置:

x = minimum
我想你的意思是:

minimum = x
看起来你的作业顺序不正确。在
A
的迭代过程中指定变量
x
不会有任何副作用

编辑

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

A = file.read().split()

但是请记住,因为您在与“比较时使用了字符串,所以我完成了它!我只是想用另一种方法把我的列表变成整数。 这是:

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()
def main():
尝试:
file=open(输入(“请输入要打开的文件名:”)
A=[]
#在这里,我将列表转换为整数,以数字形式分开,以便以后进行排序
对于文件.read().split()中的val:
A.append(int(val))
file.close()文件
n=len(A)
打印(“以下这些”,n,“数字在输入的文件中:\n”,A)
对于范围(n)中的i:
对于范围(1,n-i)内的j:
如果A[j-1]
好吧,您的代码样本格式错误。@user1906407这回答了您的问题吗?我做了更改并清理了程序,但还没有得到我想要的输出。不过我没有收到任何错误。这里是我拥有的:new_list=[],而A:minimum=A[0]表示A:if xreadlines()希望返回的数组中的元素用
\n
分隔。尝试这样做:
A=file.read().split()
['-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']
A = map(int, file.read().split())
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]
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()