@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. C++语言基础(10)-虚继承

    一.产生背景 先看下列一份代码: //间接基类A class A{ protected: int m_a; }; //直接基类B class B: public A{ protected: int m ...

  2. oracle导出表的办法

    1.先进行表分析(一定要执行此步,否则查询空表可能不准确) select 'analyze table '||table_name||' compute statistics;' from user_ ...

  3. ROW模式的SQL无法正常同步的问题总结

    转自:http://blog.chinaunix.net/uid-20639775-id-4664792.html#_Toc29623 ROW模式的SQL无法正常同步的问题总结 一. 问题起因.... ...

  4. Discuz! X 插件开发手册

      文件命名规范 Discuz! 按照如下的规范对程序和模板进行命名,请在设计插件时尽量遵循此命名规范: 可以直接通过浏览器访问的普通程序文件,以 .php 后缀命名. 被普通程序文件引用的程序文件, ...

  5. WebMethod Session

    [WebMethod(EnableSession = true)] public static string SayHello() { LxUserContext depno = HttpContex ...

  6. 微信小程序 一些要点

    微信小程序,weixin,关于微信小程序,那些开发文档没有告诉你的-微信小程序开发资源-微信开发者平台,微信开发者社区,微信小程序开发者社区 Discuz! Team and Comsenz UI T ...

  7. Unity3D学习笔记——NGUI之UISlider

    UISlider:用于创建简单的滑动块和进度条,并且可以添加一个拇指按钮. 效果图如下: 一:使用步骤 1.从上面的效果看出,这个工具由四部分组成:背景图,进度图,进度lable显示,拇指按钮. 2. ...

  8. vs2010中显示行号

    vs2010中显示行号 工具-->选项-->文本编辑器-->所有语言-->行号~ok

  9. web安全漏洞防护

    Password type input with autocomplete enabled The autocomplete attribute works with the following &l ...

  10. Golang数组的四种声明方法

    //第一种 //var <数组名称> [<数组长度>]<数组元素> var arr [2]int arr[0]=1 arr[1]=2 //第二种 //var < ...