以下是一种可能的 Python 代码实现,您可以将其复制到您的本地开发环境中并运行:

# 读取整数列表
with open('data.txt', 'r') as file:
    integers = [int(line.strip()) for line in file]

# 按降序排序
integers.sort(reverse=True)

# 将排序后的整数写入新文件
with open('data_asc.txt', 'w') as file:
    for num in integers:
        file.write(str(num) + '
')

请确保将代码保存在与 'data.txt' 同一目录下,并在运行代码之前确保该文件存在。执行代码后,将生成一个名为 'data_asc.txt' 的文件,其中包含按降序排序的整数列表。

Python 编程:将文本文件中的整数按降序排序

原文地址: https://www.cveoy.top/t/topic/UOA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录