Python活力练习Day4

Day4:将列表的值按相反顺序依次输出

        eg :  input : list = [1,2,3,4,5]

    output : [5,4,3,2,1]

方法一:时间复杂度O(n),其中 n 为列表的长度

1 def list_rev(list):
2 
3     for i in list[::-1]:
4         print(i,end = " ")
5 
6 list = [1,2,3,4,5]
7 
8 list_rev(list)

输出结果:

方法二:函数reverse()只对列表list进行操作,没有返回值。

1 def list_rev(list):
2 
3     list.reverse()  
4     for i in list:
5         print(i,end = " ")
6 
7 list = [1,2,3,4,5]
8 list_rev(list)

输出结果同上~

作为假期两天的补充~~

猜你喜欢

转载自www.cnblogs.com/xiaodangdang/p/12072913.html
今日推荐