杨辉三角是一个不能少于三行的数字群,杨辉三角的每个数字都等于它正上面和左上角数字之和。 n = 10 # 可随意改动 l = [[1 for j in range(i+1)] for i in range(n)] for i in range(2,n): # 第一、二行不处理 for j in range(1,len(l[i])-1): # 列首尾不处理 l[i][j] = l[i-1][j]+l[i-1][j-1] print(l)
[[1],
[1, 1],
[1, 2, 1],
[1, 3, 3, 1],
[1, 4, 6, 4, 1],
[1, 5, 10, 10, 5, 1],
[1, 6, 15, 20, 15, 6, 1],
[1, 7, 21, 35, 35, 21, 7, 1],
[1, 8, 28, 56, 70, 56, 28, 8, 1],
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]