请输入一段字符串,不利用反转函数,编写一段代码,将其反转。
def list_reverse(a): list_long = len(a) list_long_half = list_long // 2 # 1 2 3 1 2 3 4 1 2 3 4 5 for i in range(list_long_half): j = a[i] #a[0] a[i] = a[-i-1] #a[0]=a[-1] a[-i-1] = j #a[-1]=a[0] return a b1 = [1,2,3,4,5,6] c1 = [1,2,3,4,5,6,7] b2 = list_reverse(b1) c2 = list_reverse(c1) print(b1) print(c1) print(b2) print(c2)
运行输出
[6, 5, 4, 3, 2, 1] [7, 6, 5, 4, 3, 2, 1] [6, 5, 4, 3, 2, 1] [7, 6, 5, 4, 3, 2, 1]