通过建立二维列表a[i][j];通过控制i和j的值实现螺旋方阵。
a = []
for i in range(5):
a.append([])
for j in range(5):
a[i].append(0)#建立二维列表
b = 1
p = 5
o = 4
j=-1
k=0
for i in range(1,10):
if (i % 2 == 1):#控制行加一
for l in range(p):
if (i%4==1):
j = j + 1
else:
j = j - 1
a[k][j] = b
b = b + 1
p = p - 1
else:
for l in range(o):
if (i%4==2):
k = k + 1
else:
k = k - 1
a[k][j] = b
b = b + 1
o = o - 1
for i in range(5):
for j in range(5):
print('{:<3}'.format(a[i][j]),end=" ")
print()