已知a = [[4, 0, 1], [9, 2, 7], [8, 12, 90]]
[i for j in a for i in j]
from itertools import chain
list(chain.from_iterable(a))
sum(a, [])
import operator
from functools import reduce
print(reduce(operator.add, a))
def fun(arr, lines):
total_matrix = [[0] * lines for i in range(lines)]
""" 二维矩阵转为一维矩阵 """
num = [i for j in arr for i in j]
num = sorted(num)
show_num = 0
col = lines - 1
row = lines - 1
start_line = 0
def print_ju(start_line, col, row, show_num):
if row == 0:
if lines % 2 != 0:
total_matrix[lines // 2][lines // 2] = lines * lines
else:
for i in range(start_line, col): # 打印上横行
total_matrix[start_line][i] = num[show_num]
show_num += 1
for i in range(start_line, row, 1): # 打印右竖行
total_matrix[i][col] = num[show_num]
show_num += 1
for i in range(row, row - col + start_line, -1): # 打印下横行
total_matrix[row][i] = num[show_num]
show_num += 1
for i in range(col, col - row + start_line, -1): # 打印左边竖行
total_matrix[i][row - col + start_line] = num[show_num]
show_num += 1
return print_ju(start_line + 1, row - 1, col - 1, show_num)
print_ju(start_line, row, col, show_num)
for i in total_matrix:
for x in i:
print(format(x, '3'), end=' ')
print()
if __name__ == '__main__':
n = int(input('请输入螺旋矩阵的行数:'))
arr = [[4, 0, 1], [9, 2, 7], [8, 12, 90]]
fun(arr, n)