V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-08-22 11:50:40 +08:00 · 1400 次点击
    这是一个创建于 2045 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有一个列表 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  
       2018-08-22 14:23:25 +08:00
    指向呀
    >>>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
       2018-08-22 22:19:38 +08:00
    @dongdawang 我在 pythontutor 上执行的时候,是没有指向的。这下真的晕了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   976 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:19 · PVG 04:19 · LAX 13:19 · JFK 16:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.