Python较为便捷 , 代码量较少的实现方式如下:# -*- coding: utf-8 -*-#!/usr/bin/env pythondef triangles(): L = [1] while True: yield L L = [sum(i) for i in zip([0]+L, L+[0])]该方式用到了列表生成式 , 理解起来较困难 , 下面是另一种方式:def triangles(): ret = [1] while True: yield ret for i in range(1, len(ret)): ret[i] = pre[i] + pre[i - 1] ret.append(1) pre = ret[:]另一个不用生成器的版本:【三角形矩阵 杨辉三角】def YangHui (num = 10): LL = [[1]] for i in range(1,num): LL.append([(0 if j== 0 else LL[i-1][j-1])+ (0 if j ==len(LL[i-1]) else LL[i-1][j]) for j in range(i+1)]) return LL