Python基础课:定义一个函数,可以对序列逆序的输出(对于列表和元组可以不用考虑嵌套的情况)

时间:2021-12-21 01:48:06
 1 15 def fun(arg):
 2 16     if type(arg) is not tuple \
 3 17        and type(arg) is not str \
 4 18        and type(arg) is not list:
 5 19         print('请输入一个序列')
 6 20         return None
 7 21 
 8 22     tmp_list = list(arg)
 9 23     tmp_list.reverse()
10 24     
11 25     if type(arg) == tuple:
12 26         arg = tuple(tmp_list) 
13 27     elif type(arg) == str:
14 28         arg = ''.join(tmp_list)   
15 29     elif type(arg) == list:
16 30         arg = list(tmp_list)
17 31         
18 32     
19 33     return arg