推荐学习书目
› 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
danzzzz
V2EX  ›  Python

关于浅拷贝的一个小问题。

  •  
  •   danzzzz · Aug 22, 2018 · 2630 views
    This topic created in 2971 days ago, the information mentioned may be changed or developed.
    有一个列表 a = [1,[2,3]]
    b = list(a)
    通过以上方式完成浅拷贝之后,为什么列表 b 的 b[0]不指向列表 a 的 a[0]呢,如果说是因为整形是不可变对象因此而导致的结果的话。修改一下 a 列表。
    a = [1,[2,3],(4,5)]
    b = list(a)
    我发现当我再添加一个不可变对象元组的时候,b[2]指向了 a[2]指向的(4,5),我看到这里的时候一下子就懵了。
    小的非常想要得到解答,如果有大哥可以帮忙解答的话,真的非常感激。
    dongdawang
        1
    dongdawang  
       Aug 22, 2018
    指向呀
    >>>c = [1, [2, 2]]
    >>>d = list(c)
    >>>id(c), id(d)
    (76898120, 77072456)
    >>>id(c[0]), id(d[0]), id(c[1]), id(d[1])
    (1470780192, 1470780192, 77045704, 77045704)
    danzzzz
        2
    danzzzz  
    OP
       Aug 22, 2018
    @dongdawang 我在 pythontutor 上执行的时候,是没有指向的。这下真的晕了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5403 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:11 · PVG 17:11 · LAX 02:11 · JFK 05:11
    ♥ Do have faith in what you're doing.