@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. Pmon (LS1B)start.s

    loongson ls1b FPGA验证 只有DDR flash UART pmon移植 和原版本相比,DDR controller和ls1b不一致. /* $Id: start.S,v 1.1.1. ...

  2. python装饰器的理解

    学习python,发现装饰器是一个比较难理解的地方. 下面用代码来说明. 装饰器的作用是为了切面编程(AOP).这种编程在java上有很多实现方式.下面直接说明吧: 1.作为装饰器的函数至少有两个de ...

  3. 一个页面弄懂 CSS 样式选择器

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. rem的使用方法

    首先写入一下代码 <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" cont ...

  5. Linux Linux程序练习三

    /* index1 = 45 index2 = 36 index3 = 231 index4 = 43 index5 = 100 index6 = 123 index7 = 51 * * 通过读取读取 ...

  6. 我的第六个java程序 spring-bean

    配置文件 Beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&quo ...

  7. codeforces(559C)--C. Gerald and Giant Chess(组合数学)

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. hdu 4421(枚举+2-sat)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4421 思路:枚举32位bit,然后2-sat判断可行性,这里给出2-sat矛盾关系构图: 1.a&am ...

  9. 枚举callback还是返回列表 ?

    一般都会碰到这样的一个问题,A模块需要返回一系列的object或者message,这样一般有两种处理方式: 1,枚举callback typedef (*callback_type)(obj_type ...

  10. day5笔记

    一.上节回顾: 1,find通过元素找索引,可切片,找不到返回-12,index,找不到报错.3,split 由字符串分割成列表,默认按空格.4,captalize 首字母大写,其他字母小写.5,up ...