推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
zgoing
V2EX  ›  Python

python 小白,碰到一个坑,求助

  •  
  •   zgoing · Apr 29, 2015 · 4951 views
    This topic created in 4213 days ago, the information mentioned may be changed or developed.

    问题简化一下,是这样的

    a = []
    b = []
    for i in range(10):
        b.append(i)
        a.append(b)
        print(a)
        print()
    

    两个数组,a 每次都增加一个数组 b,结果应该是[[0]], [[0], [0,1]], [[0], [0, 1], [0, 1, 2]].......
    可是实际结果是a里的每个元素都相等,成了 [[0]], [[0, 1], [0, 1]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]
    实在搞不懂,求大神帮助!!!

    22 replies  •  2015-04-30 16:05:22 +08:00
    zgoing
    zgoing
    OP
    Apr 29, 2015
    1
    缩进错了不显示了,其实应该是从第四行起全部缩进
    Mush
    Mush
    Apr 29, 2015
    2
    没缩进的Python代码是完全没法看的......
    Livid
    Livid
    MOD
    PRO
    Apr 29, 2015❤️1
    3
    @zgoing 为你更新了一下帖子,下次请用 Markdown 发帖接口:

    https://www.v2ex.com/new

    并使用 Markdown 语法,这样帖子中的代码就可以高亮显示了。
    cloverstd
    cloverstd
    Apr 29, 2015
    4
    因为 b 是一个数组,是引用
    id(a[0]) == id(b)
    cloverstd
    cloverstd
    Apr 29, 2015❤️2
    5
    关键字应该是 python deep copy
    staticor
    staticor
    Apr 29, 2015❤️2
    6
    改成 append(b[:])
    zgoing
    zgoing
    OP
    Apr 29, 2015
    7
    @staticor 非常感谢,解决了!!
    zgoing
    zgoing
    OP
    Apr 29, 2015
    8
    @Livid 谢谢!!
    goodryb
    goodryb
    Apr 29, 2015❤️3
    10
    你每次都是把b添加进去,相当于a[0]=b,a[1]=b ... a[n]=b,所以a中的每个元素都是相同的

    假设b=[0,1,2],a.append(b)和a.append([0,1,2])是不一样的,看下面的例子

    >>> b=[0,1,2]
    >>> b
    [0, 1, 2]
    >>> a=[]
    >>> a.append(b)
    >>> a.append([0,1,2])
    >>> a
    [[0, 1, 2], [0, 1, 2]]
    >>> b.append(3)
    >>> a
    [[0, 1, 2, 3], [0, 1, 2]]
    >>>
    twor2
    twor2
    Apr 29, 2015
    11
    zgoing
    zgoing
    OP
    Apr 29, 2015
    12
    @goodryb 感觉这个真是坑。。。
    zgoing
    zgoing
    OP
    Apr 29, 2015
    13
    @twor2 这个好!不用import copy
    zgoing
    zgoing
    OP
    Apr 29, 2015
    14
    @goodryb
    @twor2
    为什么在类里面 b.copy() 不管用呢
    zgoing
    zgoing
    OP
    Apr 29, 2015
    15
    @lixia625 这个太具体了
    hansnow
    hansnow
    Apr 29, 2015 via iPhone
    16
    我记得我在javascript里也遇到过类似的问题,原因就是b只是个地址
    Andrewliu
    Andrewliu
    Apr 29, 2015
    17
    深浅拷贝问题,
    fanta
    fanta
    Apr 30, 2015
    18
    还是你没有完理解Python 所至.
    zgoing
    zgoing
    OP
    Apr 30, 2015
    19
    @fanta 嗯,是的,才学python
    fanta
    fanta
    Apr 30, 2015
    20
    @zgoing 可以参考: https://github.com/qyuhen/book Python 笔记.
    FindHao
    FindHao
    Apr 30, 2015
    21
    基本类型和引用类型的区别?
    zgoing
    zgoing
    OP
    Apr 30, 2015
    22
    @fanta 好的,已经下了,准备好好学习!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4253 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 07:42 · PVG 15:42 · LAX 23:42 · JFK 02:42
    ♥ Do have faith in what you're doing.