立即学习:https://edu.csdn.net/course/play/24797/282232?utm_source=blogtoedu
size = 6 #数值可以随意转换 array = [[0]*size] for i in range(size -1): #创建一整排空列表组 array += [[0]*size] orient = 0 #0代表向下,1表示向右,2表示向左,3表示向上 h,l = 0,0 #h表示行,l表示列 for i in range(1,size*size+1): array[h][l] = i if h+l == size-1: if h>l: orient = 1 else: orient = 2 elif h==l and h>=size/2: orient = 3 elif l-h==1 and l<=size/2: orient = 0 if orient == 0: #向下 h += 1 elif orient == 1: #向右 l += 1 elif orient == 2: #向左 l -= 1 if orient == 3: #向上 h -= 1 for ele in array: for e in ele: print('%02d'%e,end=' ') #对e格式化,每个元素用空格隔开 print('') #每段列表结束换行