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
niuoh
V2EX  ›  Python

关于 python list 赋值的一个问题!

  •  
  •   niuoh · 2016-05-08 18:46:51 +08:00 · 3369 次点击
    这是一个创建于 2920 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看图!

    给个解释啊...

    17 条回复    2016-05-09 00:18:16 +08:00
    kendetrics
        1
    kendetrics  
       2016-05-08 18:53:59 +08:00
    赋值赋过去的是入口地址呗,有啥难理解的
    想要克隆 a 的内存用 b = a[:] 就行了
    niuoh
        2
    niuoh  
    OP
       2016-05-08 19:00:10 +08:00
    @kendetrics 原来如此
    flyaway
        3
    flyaway  
       2016-05-08 19:02:01 +08:00
    Python 中全是引用……
    niuoh
        4
    niuoh  
    OP
       2016-05-08 19:03:49 +08:00
    @kendetrics 那 字典 怎么克隆内存而不是引用啊
    kendetrics
        5
    kendetrics  
       2016-05-08 19:08:13 +08:00
    @niuoh b = dict(a) 多用用搜索引擎,兄弟
    Allianzcortex
        6
    Allianzcortex  
       2016-05-08 19:09:00 +08:00   ❤️ 5
    Python 的内存机制是引用计数。对 a=[1,2,3],实际上是在内存里有一个 [1,2,3],然后增添一个引用, reference_count+1,指向 a;

    b=a,则 [1,2,3] 的 reference_count 再+1,指向 b

    b.remove(1) 修改的是它所指向的对象:由[1,2,3]变为[2,3];而 a 和 b 指向的是同一个元素,都显示[2,3]

    ---

    顺便说一下  @kendetrics 的拷贝是怎么回事

    Python 里有两种拷贝方式, shallow copy 和 deep copy

    如果你想 a 和 b 完全隔离开,用这种方式:
    import copy
    b=copy.deepcopy(a)

    如果你用 b=a[:] 这种方式,实际上还是浅拷贝,和 b=list(a) 工厂函数效果是一样的。

    这样的话用 b.remove(1) ,a 不受影响;这是因为 a,b 里面都是数字,是 immutable 不可变对象;

    如果这样,a=[1,2,[3,4]],b=a[:],你修改 a[2].append(5),则 a=[1,2,[3,4,5]],b 也同样变化, b=[1,2,[3,4,5]],因为 a[2]是一个 list,是 mutable  可变对象
    niuoh
        7
    niuoh  
    OP
       2016-05-08 19:09:24 +08:00
    @kendetrics 我觉得这里比搜索引擎好用! 哈哈
    Allianzcortex
        8
    Allianzcortex  
       2016-05-08 19:10:42 +08:00
    我觉得我已经把这一块讲完了……其他的部分 LZ 自己去 SO 搜吧

    擦……为什么是这种拍板,空格怎么变的这么大
    niuoh
        9
    niuoh  
    OP
       2016-05-08 19:11:38 +08:00
    @Allianzcortex 看完了 真·大神
    Allianzcortex
        10
    Allianzcortex  
       2016-05-08 19:12:50 +08:00
    @ niuoh 这种问题没有比 SO 讲的更好的。实际上这是伸手党问题。说的不好听,这是对自己和别人时间的不负责任。我要不是实在论文写不下去了是不会回复的
    hxndg
        11
    hxndg  
       2016-05-08 19:18:57 +08:00
    @niuoh 真心建议自己搜索。。。从我个人经验来说,搜了很久没搜到再来问茅塞顿开的效果比直接问好得多。
    zqhong
        12
    zqhong  
       2016-05-08 19:24:54 +08:00
    问题一: python list 赋值的一个问题
    用 Python 中的内置函数 id 很容易理解。




    ===

    问题二:字典怎么克隆内存而不是引用
    使用 Python 中的 标准库 - copy
    https://docs.python.org/2/library/copy.html



    ===

    关于这方面,可以再试试搜索: Python 浅拷贝 深拷贝(英文: Python deep copy shallow copy )

    推荐阅读:
    http://stackoverflow.com/questions/17246693/what-exactly-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignm
    zqhong
        13
    zqhong  
       2016-05-08 19:29:33 +08:00
    第二个图发错了。

    图二:


    我也同意实在没办法再问别人。
    Allianzcortex
        14
    Allianzcortex  
       2016-05-08 19:38:20 +08:00   ❤️ 2
    @zqhong dict 自带 copy ,可以考虑用 d2=d1.copy()~

    如果要完全分离的话,或者避免在原地址上操作,或者用 deepcopy ……
    zqhong
        15
    zqhong  
       2016-05-08 19:51:38 +08:00
    @Allianzcortex 谢谢建议!野生的 Python 选手,还有很多要学些呀~
    Allianzcortex
        16
    Allianzcortex  
       2016-05-08 19:55:00 +08:00
    @zqhong 大家都是野生程序猿 2333
    alexapollo
        17
    alexapollo  
       2016-05-09 00:18:16 +08:00
    @Allianzcortex 解答的好详细,以前我也遇到过这个问题,一直没有深究
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1213 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:58 · PVG 01:58 · LAX 10:58 · JFK 13:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.