# 生成一个1到20的列表
a=[x for x in range(1,21)]
# 把a列表切片,并赋值给b列表,x为0到20且步长为3的列表,在这里具体为[0,3,6,9,12,15,18]
# 把x带入,b列表为[a[0:0+3],a[3:3+3]……[a[18:18+3]
b=[a[x:x+3] for x in range(0,20,3)]
print(a)
print(b)
输出为
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]]