Python 列表赋值 BUG - 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
lz24xg

Python 列表赋值 BUG

  •  
  •   lz24xg Oct 19, 2022 2177 views
    This topic created in 1305 days ago, the information mentioned may be changed or developed.

    s = [1,2,3,4,[5,5,5,5]] candle = {'h':9, 'l':7, 'c':6}

    print(s[-2:]) s[-1][2] = candle['h'] s[-1][3] = candle['l'] print(s[-2:]) s.append(s[-1]) print(s[-2:]) s[-1][2] = candle['c'] s[-1][3] = candle['c'] print(s[-3:])

    输出: [4, [5, 5, 5, 5]] [4, [5, 5, 9, 7]] [[5, 5, 9, 7], [5, 5, 9, 7]] [4, [5, 5, 6, 6], [5, 5, 6, 6]]

    我改了 list 最后一个元素的值,因为后 2 个元素是相同的值,所以倒数第 2 个也被修改了?这是 Python 的 BUG 吗?

    9 replies    2022-10-19 15:20:08 +08:00
    ranleng
        1
    ranleng  
       Oct 19, 2022
    代码看的头疼,
    大概率是同一个对象导致的
    learningman
        2
    learningman  
       Oct 19, 2022
    请自行复习 值与引用
    lizytalk
        3
    lizytalk  
       Oct 19, 2022
    s.append(s[-1])之后,s 中的后两个元素指向的是相同的 list 对象
    superrichman
        4
    superrichman  
       Oct 19, 2022
    你 append 的是一个指针,s[-1]和 s[-2]都指向同一个 list 。改成 s.append(s[-1] * 1) 创建一个新的对象
    huangzhe8263
        5
    huangzhe8263  
       Oct 19, 2022
    use reference 和 use value 的问题

    记得有一个可视化这个问题的网站,我去翻翻
    lz24xg
        8
    lz24xg  
    OP
       Oct 19, 2022
    感谢,解决了
    TimePPT
        9
    TimePPT  
    PRO
       Oct 19, 2022
    In [1]: s = [1,2,3,4,[5,5,5,5]]

    In [2]: candle = {'h':9, 'l':7, 'c':6}

    In [3]: s[-1][2] = candle['h']

    In [4]: s[-1][3] = candle['l']

    In [5]: s.append(s[-1])

    In [6]: s[-1][2] = candle['c']

    In [7]: s[-1][3] = candle['c']

    In [8]: for i in s:
    ...: print(f"{i=}, {type(i)=}, {id(i)=}")
    ...:
    i=1, type(i)=<class 'int'>, id(i)=4549804272
    i=2, type(i)=<class 'int'>, id(i)=4549804304
    i=3, type(i)=<class 'int'>, id(i)=4549804336
    i=4, type(i)=<class 'int'>, id(i)=4549804368
    i=[5, 5, 6, 6], type(i)=<class 'list'>, id(i)=4582898496
    i=[5, 5, 6, 6], type(i)=<class 'list'>, id(i)=4582898496
    About     Help     Advertise     Blog     API     FAQ     Solana     2933 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 15:04 PVG 23:04 LAX 08:04 JFK 11:04
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86