请从命令行参数获取 多个整数字符串并将它们转为整数并保存到List中并按倒序、从大到小的顺序显示出来
以下是Python语言实现的示例代码:
import sys
# 从命令行参数获取多个整数字符串
int_str_list = sys.argv[1:]
# 将整数字符串转为整数并保存到List中
int_list = []
for int_str in int_str_list:
int_list.append(int(int_str))
# 按倒序、从大到小的顺序显示List中的元素
int_list.sort(reverse=True)
for num in int_list:
print(num)
使用示例:
假设将上述代码保存为sort_integers.py,然后在命令行中输入如下命令:
python sort_integers.py 5 1 7 3
输出结果为:
7
5
3
1
原文地址: https://www.cveoy.top/t/topic/bSoA 著作权归作者所有。请勿转载和采集!