这是普通的写法:
- '''
-
Pascal's triangle:
-
-
for x in pascals_triangle(5):
-
print('{0:^16}'.format(x))
-
-
[1]
-
[1, 1]
-
[1, 2, 1]
-
[1, 3, 3, 1]
-
[1, 4, 6, 4, 1]
- ''
- ## {{{ http://code.activestate.com/recipes/577542/ (r5)
-
def pascals_triangle(n):
-
x=[[1]]
-
for i in range(n-1):
-
x.append([sum(i) for i in zip([0]+x[-1],x[-1]+[0])])
-
return x
- ## end of http://code.activestate.com/recipes/577542/ }}}
另一种风格:
- def pascals_triangle(n):
-
x=[[1]]
-
for i in range(n-1):
-
x.append(list(map(sum,zip([0]+x[-1],x[-1]+[0]))))
- return x
最牛逼的:
- pascals_triangle=lambda n:[[i for i in str(11**j)] for j in range(n)]