The following small series for you to bring a python generator generation Yang Hui triangle method (must see). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Writing fun programs in Python feels like a dick, can't stop
#生成器生成展示杨辉三角 # The principle is to display the Yang Hui triangle in a 2-D array, empty place with 0, output, convert to "Def Yang (line): n,leng=0,2*line-1 f_list = List (leng +2) #预先分配, insert initial hu will slow down, the bottom line, left and right there are 1 spaces #全部初始化为0 for i,v in Enumerate (f_list): f_list[v] = 0 Zerolist = f_list[:] #预留一个全零的数组 F_LIST[LENG//2] = 1 #初始的第一行 re_list =f_list[:] n=0 while N < line:< C10/>n = n+1 yield re_list f_list,re_list = re_list[:],zerolist[:] start = Leng//2-n #计算一行中第一个1的位置 end = start + 2*n #计算一行中最后一个1的位置 while start <= end: Re_list[start] = f_list[start-1] + f_list[start+ 1] #不管是不是1, the position of the number, is the last line of the position of the left and right two numbers and start = start + 1 return ' Done ' def printlist (L): n = 0 p_str = " C21/>for value in L: ch = str (value) if value = = 0: ch = " p_str = p_str + ch print (P_STR) for V Alue in Yang (8): printlist (value)