def sortall(string):
if not string:
return []
return (sortall([x for x in string[1:] if x < string[0]])
+ [string[0]] +
sortall([x for x in string[1:] if x >= string[0]]))
file = open("sorting.txt", "r+")
for line in file.readline():
xd = (''.join(sortall(line)))
file2 = open("sorting.txt", "w")
file2.write(xd)
我如何使它成为我的代码打印排序文件行,但当它这样做时,它会按升序进行
这是一种更简洁的方法:
正如您在这里看到的:https://docs.python.org/3/howto/sorting.html,您可以提供一个callable来定制排序
读取整个文件,排序并写入新文件:
如果“排序”应该使用一些特殊的排序,请在“key”参数中提供比较函数:
相关问题 更多 >
编程相关推荐