方法一
n = int(input("请输入数字:"))
list1 = []
list2 = []
s = 0
for k in range(1,n*n+1):
s+=1
if s<=n:
list1.append(k)
if s == n:
list2.append(list1)
s = 0
list1 = []
for k1 in list2:
print(k1)
print("-"*10)
for i in range(0,len(list2)):
for j in range(0,i):
list2[i][j], list2[j][i] = list2[j][i], list2[i][j]
for k2 in list2:
print(k2)
方法二
n = int(input("n = "))
List1 = [x for x in range(1, n*n+1 )]
List2 = [List1[x:x + n] for x in range(0, len(List1), n)]
print(List2)
print(list(zip(*List2)))
- 此处为节省代码没有添加换行,换行方法可以参照方法一