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]
Something haunts me in Python的更多相关文章
- python瓦登尔湖词频统计
#瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...
- Python创建二维数组(关于list的一个小坑)
0.目录 1.遇到的问题 2.创建二维数组的办法 3.1 直接创建法 3.2 列表生成式法 3.3 使用模块numpy创建 1.遇到的问题 今天写Python代码的时候遇到了一个大坑,差点就耽误我交作 ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- python中的list的*运算使用过程中遇到的问题
目的: 想生成一个[[],[],[]] 这样的列表, 所以就 [[]]*3 这样做了,但是这样做会有问题,这样list中的三个list其实是同一个list. 例如:a=[[]]*3,然后a[0].ap ...
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
随机推荐
- 矩阵中的路径 剑指offer65题
include "stdafx.h" #include<vector> #include<algorithm> #include<string> ...
- RPMforge(Repoforge)源
centos使用rpmforge-release 时间:2017-10-09 09:48:29 阅读:536 评论:0 收藏:0 [点我收藏+] 标签:rpmf ...
- 浅谈push推送的一点感受
在手机已成为生活必不可分的一部分,push服务伴随而来.ios的apns,android随着谷歌退出中国市场,各家在android的推送不断展开.有厂商的推送,如小米.华为.魅族.oppo等,还有中间 ...
- php 图片上传 文件上传 大小 限制
nginx 413 Request Entity Too Large Php无法上传文件 查看php脚本运行用户,写个php脚本 <?php echo shell_exec("id ...
- OC 内存管理-01
创建对象 .分配内存空间,存储对象 .初始化成员变量 .反回对象的指针地址 //过程 ()对象在完成创建的同时,内部会自动创建一个引用计数器,这个计数器,是系统用来判断是否回收对象的唯一依据, 当我们 ...
- CI的意思
Continuous integration (CI) is the practice, in software engineering, of merging all developer worki ...
- 椭圆参数方程中的θ(离心角Theta)
椭圆参数方程中的离心角θ是交以其x轴对应外接圆上点的角度(或是交以其y轴对应内接圆上点的角度) 椭圆的参数程为:x=acosθy=bsinθ.M(x,y)椭圆上一点.过M作直线⊥X轴,交以O为圆心,以 ...
- yii 国际化
http://www.yiichina.com/doc/guide/2.0/tutorial-i18n config/main.php 外层加 'language' => 'en-US', 's ...
- Type Group(类型组)
在 APAP 程序开发中, 经常需要定义一些常量或变量, 而且可能存在多个程序中需要用到的类似的变量或结构体, SAP 提供了类型组, 允许用户建立一些公用的对象, 允许在不同的程序中调用, 这样不但 ...
- Android无线测试之—UiAutomator UiObject API介绍二
点击与长按 一.组件区域位置关系 Rect 对象代表一个矩形区域 [Left,Top] [Right,Bottom] 二.点击与长按API 返回值 API 描述 boolean click() 点击对 ...