Discuss / Python / 借鉴一下你们的

借鉴一下你们的

Topic source

test_number

#1 Created at ... [Delete] [Delete and Lock User]

def triangles(n): t=[1] #初始列表 x=0 #x作为列表数判断 while x<=n: #判断有多少个列表 yield t #输出generator t=[t[i-1]+t[i] for i in range(len(t))] #数列生成式,后一列的第i个元素等于上一列表第i-1和第i个元素的之和,元素数量计算用len()函数 t[0]=1 #规定每列第一个元素必须为1 t.append(1) #设置每列最后都加上1 x=x+1

for i in triangles(10): print(i)


  • 1

Reply