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()
但请记住,由于您在与“进行比较时使用的是字符串,因此有以下行:
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]
我完成了!我只是想用另一种方法把我的列表变成整数。
这是:
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()
@user1906407这是否回答了您的问题?我进行了更改并清理了程序,但尚未获得所需的输出。不过我没有收到任何错误。这里是我拥有的:new_list=[],而A:minimum=A[0]表示A:if xdef 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()