Something haunts me in Python
@1: 在查看"The Python Library Reference"(https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange)
的时候发现了这样的一段代码:
代码1:
>>> lists = [[]] * 3 >>> lists [[], [], []] >>> lists[0].append(3) >>> lists
执行完lists[0].append(3)之后,程序将输出什么结果? [[3], [0], [0]]?
正确答案是[[3], [3], [3]],让我们来看看Reference上的解释:
This often haunts new Python programmers. What has happened is that [[]] is a one-element list containing
an empty list, so all three elements of [[]] * 3 are (pointers to) this single empty list. Modifying any of the elements
of lists modifies this single list. You can create a list of different lists this way:
代码2:
>>> lists = [[] for i in range(3)] >>> lists[0].append(3) # 此时lists为[[3], [], []] >>> lists[1].append(5) >>> lists[2].append(7) >>> lists [[3], [5], [7]]
补充:代码1中lists的三个元素都指向同一个空list,是因为:s * n, n * s --- n shallow copies of s concatenated,
即Python中的*运算采用的是浅复制。
@2: Slicing & Slice Assignment(http://stackoverflow.com/questions/10623302/how-assignment-works-with-python-list-slice/10623352#10623352)
1. slicing:
b = a[0:2]
This makes a copy of the slice of a and assigns it to b.
2. slice assignment:
a[0:2] = b
This replaces the slice of a with the contents of b.
Although the syntax is similar (I imagine by design!), these are two different operations.
@3: 针对上面silce assignment的例子进行进一步分析:
>>> a = [1, 4, 3] >>> b = [6, 7] >>> a[1:3] = b >>> a [1, 6, 7] >>> b [6, 7] >>> a[1] = 0 >>> a [1, 0, 7]
此时b的值是多少?
>>> b [6, 7] >>>
让我们继续:
代码1:
>>> a[0:3] = b #长度不同,也允许 >>> a [6, 7] >>> b [6, 7] >>> a[1] = 1 #这种情况, 改变a不会影响b >>> a [6, 1] >>> b [6, 7] >>> b[1] = 8 #这种情况, 改变b不会影响a >>> b [6, 8] >>> a [6, 1]
代码2:
>>> b = [6, 7] >>> c = b >>> c [6, 7] >>> b[0] = 0 >>> b [0, 7] >>> c [0, 7] >>> c[0] = 10 >>> b [10, 7] >>> c [10, 7]
比较代码1和代码2结果的不同,进一步理解slice assignment。
代码3: slicing
>>> a = [1, 2, 3, 4] >>> b = a[:2] >>> a [1, 2, 3, 4] >>> b [1, 2] >>> b[0] = 9 >>> b [9, 2] >>> a [1, 2, 3, 4]
转载于:https://www.cnblogs.com/lxw0109/p/note_in_python.html
Something haunts me in Python相关推荐
- Python创建二维数组(关于list的一个小坑)
0.目录 1.遇到的问题 2.创建二维数组的办法 3.1 直接创建法 3.2 列表生成式法 3.3 使用模块numpy创建 1.遇到的问题 今天写Python代码的时候遇到了一个大坑,差点就耽误我交作 ...
- python 创建二维list,Python创建二维数组(关于list的一个小坑)
遇到的问题 今天写Python代码的时候遇到了一个大坑,问题是这样的,我需要创建一个二维数组,如下: 输出结果如下: 是不是看起来没有一点问题? 一开始我也是这么觉得的,以为是我其他地方用错了什么函数 ...
- python 画心_python画心性线
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 的确如此,那是极坐标系,python 的matplotlib同样支持极坐标系的, ...
- 在python环境下使用Openposes
Python调用Openpose 1. python环境的openpose编译 在cmake的过程中需要设置: DBUILD_PYTHON=ON 可以直接在openpose工程文件夹下面测试和修改py ...
- python创建全为0的二维列表遇到的坑
本来想着简单点,用列表乘法 m = n = 3 test = [[0] * m] * n print(test) 输出也看了一下,没啥问题 [[0, 0, 0], [0, 0, 0], [0, 0, ...
- Github配置(git+vscode+python+jupyter)
①下载git 打开 git bash 工具的用户名和密码存储 $ git config --global user.name "Your Name" $ git config -- ...
- 【实验楼】python简明教程
①终端输入python进入 欣赏完自己的杰作后,按 Ctrl + D 输入一个 EOF 字符来退出解释器,你也可以键入 exit() 来退出解释器. ②vim键盘快捷功能分布 ③这里需要注意如果程序中 ...
- 【Kaggle Learn】Python 5-8
五. Booleans and Conditionals Using booleans for branching logic x = True print(x) print(type(x))''' ...
- 【Kaggle Learn】Python 1-4
[Kaggle Learn]Python https://www.kaggle.com/learn/python 一. Hello, Python A quick introduction to Py ...
最新文章
- html分页自动加载数据库,AngularJS实现分页显示数据库信息
- 常见的数据库端口及查询方法
- Spring Boot 注解大全,一键收藏!回城路上复习!
- 一天一道算法题--5.30---递归
- Scrapy定向爬虫教程(三)——爬取多个页面
- 三维重建2: 地图构建-三角测量
- 使用SAP CRM WebClient UI Design layer修改field label
- 如何向妻子解释OOD(转)
- tomcat以debug模式启动
- [RK3399][Android7.1] 调试笔记 --- 查看当前DDR的工作频率
- 董事长、CEO、总裁、总经理、总监的区别
- winpe修复计算机无法启动,巧用PE修复系统启动故障
- 织梦主要文件夹目录及模板文件说明
- 《最好的告别》是有尊严的离开
- 误删除文件怎么找回 数据恢复用这些方法
- uniapp小程序获取定位(高德SDK)
- C++学习需要看的书籍
- C# 最新手机号码段的号码验证实现
- 视频教程-Android Studio 开发详解-Android
- sgu 187 Twist and whirl - want to cheat 伸展树(splay)
热门文章
- java cpu 监控工具_Java自带的GUI性能监控工具Jconsole以及JisualVM简介
- 设计模式(三)--适配器模式
- mysql group by join_mysql – GROUP BY之后的LEFT JOIN?
- ws配置 zuul_SpringCloud系列研究---服务网关zuul
- console修改 ajax,【快速】chrome中console下ajax访问后台
- PyTorch教程(一):张量数据类型
- 从零开始搭建spring-cloud(5) ----zuul
- 年终复盘刚需!Python数据可视化技巧来了
- 快手公司厕所装计时器,网友:再也不能带薪拉屎了!
- 涨姿势了,raise...from... 是个什么操作?