快捷搜索:  汽车  科技

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨2.1 杨辉三角形,即Pascal Triangle=帕斯卡三角形。========1.3 代码讲解通俗易懂,注释仔细,小白秒懂。1.4 环境:python3.82 杨辉三角

1 说明

=====

1.1 杨辉三角的介绍。

1.2 杨辉三角的python实现,用turtle和pydotplus高级别可视化实现。

1.3 代码讲解通俗易懂,注释仔细,小白秒懂。

1.4 环境:python3.8

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨(1)

2 杨辉三角

========

2.1 杨辉三角形,即Pascal Triangle=帕斯卡三角形。

2.2 又称贾宪三角形,是二项式系数在三角形中的一种几何排列。

2.3 北宋人贾宪约1050年首先使用“贾宪三角”进行高次开方运算。

2.4 南宋数学家,杨辉所著的《详解九章算术》(1261年)一书中用如图的三角形解释二项和的乘方规律。

2.5 规律:在杨辉三角中

第3行的三个数恰好对应着两数和的平方的展开式的每一项的系数,

(a b)²;=a² 2ab b²

第4行的四个数恰好依次对应两数和的立方的展开式的每一项的系数,

(a b)³=a³ 3a²b 3ab² b³

以此类推。

因此可得出二项式定理的公式为:

(a b)ⁿ=C(n 0)aⁿ×bº C(n 1)a^(n-1)×b¹ ... C(n r)a^(n-r)×b^r... C(n n)aº×bⁿ

3 python可视化效果图赏析

===================

3.1 终端图

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨(2)

图1

3.2 turtle图

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨(3)

图2:小bug

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨(4)

图3:小bug

3.3 pydotplus图

用最简单的python写杨辉三角:介绍和python高级别可视化实现和探讨(5)

图4:经典

4 上述4张图的python的代码

=====================

4.1 图1的代码:

#参考文章 #https://blog.csdn.net/weixin_43469680/article/details/88781849?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.add_param_isCf #杨辉三角-金字塔版 ''' 注意:迭代对象 1金字塔的数字列表 2列表数值转str类型.center居中 ''' n_you=int(input('请您输入杨辉三角的层数,推荐6:')) #自己增加的 data_lb=[] #定义三角 def triangle(): N = [1] while True: # generator特点在于:在执行过程中,遇到yield就中断,下次又继续执行 yield N # 我们需要吧N复制给L 而不能直接L = N,因为这样L和N会在同一个地址,后续算法就会出错 L = N.copy() for j in range(len(L)): # 遍历和转化 temp = str(L[j]) L[j] = temp data_lb.append(temp) l = ' '.join(L).center(50) # 组合和居中一起写 print(l) # 这里就是打印l了 N.append(0) # 每次都要在最后一位加个0,用于后续的叠加 N = [N[i] N[i - 1] for i in range(len(N))] #打印三角的设置 def print_triangle(x): a = 0 for t in triangle(): # 这里可以每次调用一个N(得力于Yield函数) a = 1 if a == x: break #打印杨辉三角 print_triangle(n_you 1) # 打印7行 a1~f6 #备用:自己增加的,便于pydotplus中使用 #print(data_lb) #label_world=['a1' 'b1' 'b2' 'c1' 'c2' 'c3' 'd1' 'd2' 'd3' 'd4' 'e1' 'e2' 'e3' 'e4' 'e6' 'f1' 'f2' 'f3' 'f4' 'f5' 'f6']

4.2 图2的代码:

#参考文章 #https://blog.csdn.net/weixin_42644456/article/details/107963565?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-2-107963565.nonecase&utm_term=python杨辉三角字符输出居中&spm=1000.2123.3001.4430 import turtle as t #杨辉三角和居中 N=[1] #定义画线 def prtLine(): global N N=[1] [ N[i] N[i 1] for i in range(len(N)-1) ] [1] #杨辉三角放到二维列表中 d=[] d.append(N) for i in range(5): prtLine() d.append(N) #每一行数字拼接成一个字符串 5个空格连接 #多行内容,组成字符串列表 str_prt=[] for dataLine in d: str_prt.append(' '.join( str(v) for v in dataLine )) #文本输出的居中。可以有其他居中方法。以80为总宽度 for txt in str_prt: padding=int(( 80-len(txt))/2 ) #画图 t.pensize(3) t.penup() y=200 t.goto(0 y) for i in range(len(str_prt)): txt=str_prt[i] y-=80 # 画图模式下,一个字符的宽带是5 padding=int(( -len(txt)*5 )/2 ) t.goto(padding y) t.write(txt font=("Times" 10 "bold")) # 移动到第一个字符的下方 #调节连接符合线的位置 t.goto(padding 10 y 55) # 画折线 if i>=1 and i< len(str_prt): t.pendown() t.setheading(45) for k in range(i): t.forward(30) t.left(-90) t.forward(30) t.right(-90) t.penup() t.done()

4.3 图3代码

#蜂窝六边形添加杨辉三角数字 import turtle as t import math as m #影响杨辉三角的层数和蜂窝六边形的层数 n_you=int(input('请您输入杨辉三角的层数,推荐7:')) #杨辉三角和居中 N=[1] #画线 def prtLine(): global N N=[1] [ N[i] N[i 1] for i in range(len(N)-1) ] [1] #杨辉三角放到二维列表中 d=[] d.append(N) for i in range(n_you): prtLine() d.append(N) #每一行数字拼接成一个字符串 5个空格连接 #多行内容,组成字符串列表 str_prt=[] for dataLine in d: str_prt.append(' '.join( str(v) for v in dataLine )) t.setup(600 500 None None) def draw(): #以图形中心点为基准进行绘图扩张 for y in range(len(str_prt)): #设置列向第一个图形的坐标 pen_y =180 -45 *y pen_x =-250 7.5 *m.sqrt(3) *m.pow(-1 y) t.penup() t.goto(pen_x 180-20*(y 1) pen_y) txt=str_prt[y] t.write(txt font=("Times" 10 "bold")) t.pendown #加3是向右增加,可适当调整 for x in range(len(str_prt) 3): #设置行向图形的扩张 t.circle(30 steps=6) x1 =pen_x 30 *m.sqrt(3) *x t.penup() t.setx(x1) t.pendown() t.tracer(False) #直接获取绘图结果,省略过程 draw() t.done()

4.4 图4代码:经典

import pydotplus as pdp #语法符合原dot语法 dot = ''' //定义节点属性 digraph g { // 说实话代码太啰嗦了,要是能和python一样就好了 //==========定义节点关系============ // 左下斜 a1->b1->c1->d1->e1->f1; b2->c2->d2->e2->f2; c3->d3->e3->f3; d4->e4->f4; e5->f5; // 右下斜 a1->b2->c3->d4->e5->f6; b1->c2->d3->e4->f5; c1->d2->e3->f4; d1->e2->f3; e1->f2; //以上是默认 a1[shape=circle label="1"]; //指定圆和标签名 b1[shape=circle label="1"]; b2[shape=circle label="1"]; c1[shape=circle label="1"]; c2[shape=circle label="2"]; c3[shape=circle label="1"]; d1[shape=circle label="1"]; d2[shape=circle label="3"]; d3[shape=circle label="3"]; d4[shape=circle label="1"]; e1[shape=circle label="1"]; e2[shape=circle label="4"]; e3[shape=circle label="6"]; e4[shape=circle label="4"]; e5[shape=circle label="1"]; f1[shape=circle label="1"]; f2[shape=circle label="5"]; f3[shape=circle label="10"]; f4[shape=circle label="10"]; f5[shape=circle label="5"]; f6[shape=circle label="1"]; } ''' #调用函数数据制图 graph = pdp.graph_from_dot_data(dot) #生成jpg图片 graph.write_jpg('/home/xgj/Desktop/yhsj/4.jpg') ''' #备注 ['1' '1' '1' '1' '2' '1' '1' '3' '3' '1' '1' '4' '6' '4' '1' '1' '5' '10' '10' '5' '1'] ['a1' 'b1' 'b2' 'c1' 'c2' 'c3' 'd1' 'd2' 'd3' 'd4' 'e1' 'e2' 'e3' 'e4' 'e5' 'f1' 'f2' 'f3' 'f4' 'f5' 'f6'] '''

图4很棒,但是dot的代码太繁琐了,您有没有更好的杨辉三角python可视化的方法呢?

可以一起探讨。

猜您喜欢: