@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的更多相关文章

  1. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  2. Python创建二维数组(关于list的一个小坑)

    0.目录 1.遇到的问题 2.创建二维数组的办法 3.1 直接创建法 3.2 列表生成式法 3.3 使用模块numpy创建 1.遇到的问题 今天写Python代码的时候遇到了一个大坑,差点就耽误我交作 ...

  3. 在python中定义二维数组

    发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...

  4. python中的list的*运算使用过程中遇到的问题

    目的: 想生成一个[[],[],[]] 这样的列表, 所以就 [[]]*3 这样做了,但是这样做会有问题,这样list中的三个list其实是同一个list. 例如:a=[[]]*3,然后a[0].ap ...

  5. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  6. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  7. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  8. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  9. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

随机推荐

  1. TPM概述

    TPM(Trusted Platform Module)安全芯片,是指符合TPM(可信赖平台模块)标准的安全芯片.标准由TCG(可信赖计算组织,Trusted Computing Group)提出,目 ...

  2. Android 缓存详解目录

    1.http://www.cnblogs.com/lzrabbit/p/3734850.html 2.

  3. 什么是 HTTPS

    HTTPS (基于安全套接字层的超文本传输协议 或者是 HTTP over SSL) 是一个 Netscape 开发的 Web 协议. 你也可以说:HTTPS = HTTP + SSL HTTPS 在 ...

  4. 新MBP使用git命令时启用xcode的终端log

    Last login: Mon Oct 22 12:41:33 on consoleuser:~ me$ git Agreeing to the Xcode/iOS license requires ...

  5. 用Python和py2app写独立的Mac OS X 应用

    文/lovexiaov(简书作者)原文链接:http://www.jianshu.com/p/afb6b2b97ce9著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 前提 创建一个普通 ...

  6. Laravel5.1 路由 -基础

    什么是路由? 大K简单的说下,路由是用户访问我们程序的一个入口,比方说 你在浏览器输入:www.myblogs.com/create 就会打开一个页面,我们接收到这一个请求后后台需要做一些事儿来反馈给 ...

  7. 多用户商城系统 KgMall2.1公布

    2014-5-28日,广州JUULUU公布多用户商城系统 KgMall2.1,kgMall是国内一款JAVA开源多用户版商城系统,新版KgMall更加模块化,juuluu团队重构了Kgcms的多个模块 ...

  8. 49、android ListView几个比较特别的属性

    由于这两天在做listView的东西,所以整理出来一些我个人认为比较特别的属性,通过设置这样的属性可以做出更加美观的列表 android:stackFromBottom="true" ...

  9. 常见cout格式输出

    cout.setf(ios::fixed);//设置格式 cout.unsetf(ios::fixed);//取消格式 cout.setf(ios::scientific);//科学记数法 cout. ...

  10. 1119 机器人走方格 V2(组合)

    1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于 ...