用python实现杨辉三角

Posted quota

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python实现杨辉三角相关的知识,希望对你有一定的参考价值。

def yanghui(lines):
       currentlst,lastlst,n=[],[],1
       if lines<1:
              return
       while n<=lines:
              lastlst=currentlst
              currentlst=[]
              for i in range(n):
                     if(i==0):
                            currentlst.insert(0,1)
                     elif(i==n-1):
                            currentlst.insert(i,1)
                     else:
                            currentlst.insert(i,lastlst[i]+lastlst[i-1])
              n=n+1
              yield currentlst
              
f = yanghui(10)
for t in f:
       print(t)

  

以上是关于用python实现杨辉三角的主要内容,如果未能解决你的问题,请参考以下文章