一:考察的主要知识点:
类型的转换 :先由元组--字符串--最后到列表实现数据排序
1. 实现数据排序:
def f(*args):
print(args) # 输出的是元组 ('23,45,2,4,5',)
print(args[0],type(args)) # 23,45,2,4,5 <class 'tuple'>
o=args[0] # 字符串
# for i in o:
# print(i)
li=o.split(',') # 把字符串切割成列表,列表里面的元素都是字符转需要类型转换,然后依次加入新的列表里面实现排序
print(li) # ['23', '45', '2', '4', '5']
print('-------------------------')
oli=[] # 定义一个空列表
for i in li:
oli.append(int(i))
print(sorted(oli)) # [2, 4, 5, 23, 45]
inp=input('请输入排序的数据:') # 23,45,2,4,5
f(inp)
运行截图:
2.通过一个参数来决定是正向排序还是倒叙排序(写代码前要梳理好思路)
'''
1、定义函数def
2、接受数据 ---参数===不定长参数
3、再来一个参数来决定是正向还是反向---if
4、sorted reversed
'''
def f(i,*args):
if i: # i为True,非0,不为空 none 正向
pass
else: # 反向
pass