算法-----正序或者倒序打印列表或者数字

 1使用递归的方式升序打印这个列表[1,2,3,4,5,6]
 2 代码
 3 
 4 def func(lis):
 5     if len(lis)==0:
 6          return ""
 7     else:
 8         print(lis.pop())
 9         func(lis)
10 
11 lis=[1,2,3,4,5,6]
12 func(lis)
13 
14 执行结果
15 >>> def func(lis):
16 ...     if len(lis)==0:
17 ...          return ""
18 ...     else:
19 ...         print(lis.pop())
20 ...         func(lis)
21 ...
22 >>> lis=[1,2,3,4,5,6]
23 >>> func(lis)
24 6
25 5
26 4
27 3
28 2
29 1
30 >>>
31 使用递归的方式倒序打印这个列表[1,2,3,4,5,6]
32 代码
33 def func(lis):
34     if len(lis)==0:
35          return ""
36     else:        
37         print(lis.pop(0))
38         func(lis)
39 
40 lis=[1,2,3,4,5,6]
41 func(lis)
42 >>> def func(lis):
43 ...     if len(lis)==0:
44 ...          return ""
45 ...     else:
46 ...         print(lis.pop(0))
47 ...         func(lis)
48 ...
49 >>> lis=[1,2,3,4,5,6]
50 >>> func(lis)
51 1
52 2
53 3
54 4
55 5
56 6

 

posted @ 2020-02-17 19:58  进阶的淑琴  阅读(242)  评论(0)    收藏  举报