python3 简单实现杨辉三角

def yanghui():
    L = [1]
    while True:
        yield L
        if len(L)==1:
           L.append(1)
        else:
            # for i in range(0,len(L)-1):
                # temp[i] = L[i]+L[i+1]
            temp = [L[i] +L[i+1] for i in range(0,len(L)-1)]    
            L = [1]+temp+[1]

测试代码:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- ‘a test module’ __author__ = ‘Aran Li’ import def_yanghui def testyh(n): re= [] count = 0 for t in def_yanghui.yanghui(): print(t) re.append(t) count = count +1 if count == n: break print(re)

输出会出现[1]变成[1,1],排查不到原因

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/wakingwind/article/details/79655686
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞