简体   繁体   English

这是关于python中列表的问题。 我们可以连接两个列表

[英]This is a question about lists in python. Can we concatenate two lists

 n=int(input("Enter number of rows: "))
 a=[]
 for i in range(n):
     a.append([])
     a[i].append(1)
     for j in range(1,i):
         a[i].append(a[i-1][j-1]+a[i-1][j])
     if(n!=0):
         a[i].append(1)
 for i in range(n):
     print("   "*(n-i),end=" ",sep=" ")
     for j in range(0,i+1):
         print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
     print()

I'm trying to learn to print a pascal triangle, got struck at one line 我正在努力学习打印一个帕斯卡三角形,在一条线上被击中

a[i].append(a[i-1][j-1]+a[i-1][j])

is the above line what does the line do. 上面这行是做什么的。 can anyone explain a[i-1][j-1] does? 任何人都可以解释[i-1] [j-1]吗?

Note that a is a list of list where a[i] is the i-th row of the Pascal's triangle. 注意a是列表,其中a[i]是Pascal三角形的第i行。

a[i][j] stores the j-th element of the i-th row of the Pascal's triangle, it is an int. a[i][j]存储Pascal三角形的第i行的第j个元素,它是一个int。

They are just using the formula of Pascal's triangle . 他们只是使用Pascal三角形的公式。

a[i][j] = a[i-1][j-1] + a[i-1][j]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM