Python 数列排序:从小到大排列输入数字
以下是 Python 代码实现将用户输入的数列从小到大排序:
num_list = input('请输入数列,每项之间用空格分开:').split() # 读取输入的数列并按空格分割成列表
num_list = [int(num) for num in num_list] # 将列表中的元素转换为整数类型
num_list.sort() # 对列表进行排序
print('排序后的数列为:', end='')
for num in num_list:
print(num, end=' ')
运行程序后,输入数列,例如:
请输入数列,每项之间用空格分开:5 1 7 3 9
程序会将数列从小到大排序,并输出结果:
排序后的数列为:1 3 5 7 9
原文地址: https://www.cveoy.top/t/topic/mUXs 著作权归作者所有。请勿转载和采集!