zoukankan      html  css  js  c++  java
  • Python(四)生成器 和 杨辉三角

    学习链接:

    http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317799226173f45ce40636141b6abc8424e12b5fb27000

    生成器的东西不是很难,主要注意下list的里面listwy[-1]表示list的最后一个元素

    然后是习题比较难,我一开始摸不着头脑

    练习

    杨辉三角定义如下:

              1
             / 
            1   1
           /  / 
          1   2   1
         /  /  / 
        1   3   3   1
       /  /  /  / 
      1   4   6   4   1
     /  /  /  /  / 
    1   5   10  10  5   1
    

    把每一行看做一个list,试写一个generator,不断输出下一行的list:

    # -*- coding: utf-8 -*-
    
    def triangles():
    

    # 期待输出:
    # [1]
    # [1, 1]
    # [1, 2, 1]
    # [1, 3, 3, 1]
    # [1, 4, 6, 4, 1]
    # [1, 5, 10, 10, 5, 1]
    # [1, 6, 15, 20, 15, 6, 1]
    # [1, 7, 21, 35, 35, 21, 7, 1]
    # [1, 8, 28, 56, 70, 56, 28, 8, 1]
    # [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
    n = 0
    results = []
    for t in triangles():
        print(t)
        results.append(t)
        n = n + 1
        if n == 10:
            break
    if results == [
        [1],
        [1, 1],
        [1, 2, 1],
        [1, 3, 3, 1],
        [1, 4, 6, 4, 1],
        [1, 5, 10, 10, 5, 1],
        [1, 6, 15, 20, 15, 6, 1],
        [1, 7, 21, 35, 35, 21, 7, 1],
        [1, 8, 28, 56, 70, 56, 28, 8, 1],
        [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
    ]:
        print('测试通过!')
    else:
        print('测试失败!')

    然后在参考了一些博客之后,找到了一个相对我能理解的,并之后自己写出来的一个方法:

    def triangles():
        templist=[]
        mylist=[1]
        while True:
            yield mylist
    
            mylist.append(0)
            for n in range(len(mylist)):
                templist.append(mylist[n]+mylist[n-1])
            mylist=templist
            templist=[];

    自己去用笔算一下,

    然后注意下 range函数的作用:

    range[5]=[0,1,2,3,4]

    然后就知道该怎么样了

    4/19 跟新:

    因为昨天晚上研友着急回去,发现了上面代码的bug,但是来不及更改了,今天早上更改下:

    如果用上面的代码来写,会出现这样的情况:

    然后,我们输出下result:

    [[1, 0], [1, 1, 0], [1, 2, 1, 0], [1, 3, 3, 1, 0], [1, 4, 6, 4, 1, 0], [1, 5, 10, 10, 5, 1, 0], [1, 6, 15, 20, 15, 6, 1, 0], [1, 7, 21, 35, 35, 21, 7, 1, 0], [1, 8, 28, 56, 70, 56, 28, 8, 1, 0], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]

     可以发现,除了最后一个,所有的前面都加了个0

    当时,想了好久都没想到原因,最后baidu到是python引用传递的问题,顺便找到了一个网站:

    http://www.pythontutor.com/visualize.html#mode=edit

    这是个可视化python网站,我们可以看见,在mylist添加0之前,各个引用的指向:

    可以看见mylist和result[0]指向的是同一个位置

    然后mylist.append了后:

    然后,mylist直接指向templist:

    这就是,为什么有前面有0的原因。

    所以,我们只需要在mylist指向templist之前将这个0给pop出去就行了,测试通过代码如下:

    def triangles():
        templist=[]
        mylist=[1]
        while True:
            yield mylist
            mylist.append(0)
            for n in range(len(mylist)):
                templist.append(mylist[n]+mylist[n-1])
            mylist.pop(-1)
            mylist=templist
            templist=[];
    
    
    n = 0
    results = []
    for t in triangles():
        print(t)
        results.append(t)
        # print(results,"dfg")
        n = n + 1
        if n == 10:
            break
    if results == [
        [1],
        [1, 1],
        [1, 2, 1],
        [1, 3, 3, 1],
        [1, 4, 6, 4, 1],
        [1, 5, 10, 10, 5, 1],
        [1, 6, 15, 20, 15, 6, 1],
        [1, 7, 21, 35, 35, 21, 7, 1],
        [1, 8, 28, 56, 70, 56, 28, 8, 1],
        [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
    ]:
        print('测试通过!')
    else:
        print('测试失败!')

  • 相关阅读:
    C#取枚举描述
    JSON数组操作
    .NET DES 加密
    MVC返回图片
    with check option
    HashSet<T>类
    Repository模式
    C#泛型集合之Dictionary<k, v>使用技巧
    权限管理——在线列表
    数据库锁
  • 原文地址:https://www.cnblogs.com/William-xh/p/8877997.html
Copyright © 2011-2022 走看看