简体   繁体   中英

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?

Note that a is a list of list where a[i] is the i-th row of the Pascal's triangle.

a[i][j] stores the j-th element of the i-th row of the Pascal's triangle, it is an int.

They are just using the formula of Pascal's triangle .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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