A common misunderstanding about list.append() for newbies-----> like me
来源:互联网 发布:c语言中条件表达式 编辑:程序博客网 时间:2024/05/17 00:56
Well,list.append() is to append an element to the end of a list
However, if this element is a list too, append() will not copy this list to the newly generated list, but simply makes a reference to this list.
So, any change to this list will also change the generated one, and vice versa.
A simple test:
>>> a = ['test']*3>>> b = []>>> b.append(a)>>> b.append(a)>>> print(b)[['test', 'test', 'test'], ['test', 'test', 'test']]>>> b[0][0] = 1>>> print(b)[[1, 'test', 'test'], [1, 'test', 'test']]>>> print(a)[1, 'test', 'test']
And two more issues about this 'bug'
Case 1: http://bugs.python.org/issue8023
Case 2: http://mail.python.org/pipermail/python-bugs-list/2007-February/037113.html
- A common misunderstanding about list.append() for newbies-----> like me
- a common misunderstanding of string.Trim functions
- About me!
- About me
- About Me
- About me,
- About me......
- About Me
- About me
- Me.About()
- about me
- About Me
- about me
- About Me
- About me
- About Me
- about me
- about me
- 数据库连接池
- 为jdbc开发一个小型sql语句生成器
- 百度地图API之地图加载方式(同步、异步)
- linux中ip相关配置
- CAS SERVER返回更多用户信息给客户端
- A common misunderstanding about list.append() for newbies-----> like me
- C++字符串类型转换
- Matlab曲线拟合 最小二乘法
- Camel学习之---文件转移(HelloWorld)
- Debugging the kernel using Ftrace
- Python:itertools模块
- Gentoo技巧总结
- 黑马程序员_反射
- git使用详细介绍