Python 翻转列表
最后更新于:2022-03-27 02:36:17
Python 翻转列表
定义一个列表,并将它翻转。
例如:
翻转前 : list = [10, 11, 12, 13, 14, 15] 翻转后 : [15, 14, 13, 12, 11, 10]
实例 1
def Reverse(lst):
return [ele for ele in reversed(lst)]
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
return [ele for ele in reversed(lst)]
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
以上实例输出结果为:
[15, 14, 13, 12, 11, 10]
实例 2
def Reverse(lst):
lst.reverse()
return lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
lst.reverse()
return lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
以上实例输出结果为:
[15, 14, 13, 12, 11, 10]
实例 3
def Reverse(lst):
new_lst = lst[::-1]
return new_lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
new_lst = lst[::-1]
return new_lst
lst = [10, 11, 12, 13, 14, 15]
print(Reverse(lst))
以上实例输出结果为:
[15, 14, 13, 12, 11, 10]