我正在尝试打印[0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]
我只知道如何打印[0, 1, 2, 3, 4, 5]
values = []
for numbers in range(0, 6):
values.append(numbers)
print(values)
答案 0 :(得分:3)
range
函数的工作方式如下:
range(start, stop, step)
start
是起始点,stop
是终止点后的1,步长是如何在每次迭代中更新值。
values = []
for numbers in range(0, 6):
values.append(numbers)
for numbers in range(5, -1, -1):
values.append(numbers)
print(values)
答案 1 :(得分:0)
l = [0, 1, 2, 3, 4, 5]
l = l + l[::-1]
print l
[0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0]
答案 2 :(得分:0)
尝试一下:
count = 6
left = []
right = []
for numbers in range(count):
left.append(numbers)
right.append(count-numbers-1)
left.extend(right)
print(left)