Be careful!

The list of list is modified unexpected, python

# code patch A:
list = [1,2,3,4,5,6,7]
print('list A :', list)
for i in list:
temp = i
if i > 5:
temp = i + 10000
print('list A\':', list) # code patch B:
list = [[1],[2],[3],[4],[5],[6],[7]]
new_list = []
print('\nlist B : ',list)
for i in list:
temp = i # will this allocate a new RAM space for var 'temp'?
if i[0] > 5:
temp[0] = i[0] + 1000
new_list.append(temp)
print('list B\': ', list, end='')
print(' <-- you can see the list is changed, which is not i want.')
print('new_list:', new_list) # code patch C:
list = [[1],[2],[3],[4],[5],[6],[7]]
new_list = []
print('\nlist C : ',list)
for i in list:
temp = [''] # only this line is MODIFIED than code patch B.
temp[0] = i[0]
if i[0] > 5:
temp[0] = i[0] + 1000
new_list.append(temp)
print('list C\': ', list, end='')
print(' <-- you can see the list is NOT changed.')
print('new_list:', new_list) '''
Output:
---
list A : [1, 2, 3, 4, 5, 6, 7]
list A': [1, 2, 3, 4, 5, 6, 7] list B : [[1], [2], [3], [4], [5], [6], [7]]
list B': [[1], [2], [3], [4], [5], [1006], [1007]] <-- you can see the list is changed. which is not i want.
new_list: [[1], [2], [3], [4], [5], [1006], [1007]] list C : [[1], [2], [3], [4], [5], [6], [7]]
list C': [[1], [2], [3], [4], [5], [6], [7]] <-- you can see the list is NOT changed.
new_list: [[1], [2], [3], [4], [5], [1006], [1007]] Question:
why list B' is modified in the for loop? '''

The list of list is modified unexpected, python的更多相关文章

  1. Python实践:模块自动重载

    一.概述 二.思路 三.实现 四.测试 1.开启自动重载(终端1) 2.修改模块(终端2) 3.查看实时输出(终端1) 五.参考源码 一.概述 开发Web程序时,通常会采用本地服务器进行调试,但如果代 ...

  2. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...

  3. Python关键点笔记之使用 pyenv 管理多个 Python 版本依赖环境

    0x00 背景 从接触Python以来,一直都是采用virtualenv和virtualenvwrapper来管理不同项目的依赖环境,通过workon.mkvirtualenv等命令进行虚拟环境切换, ...

  4. Python:渗透测试开源项目【源码值得精读】

    sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网 ...

  5. Linux——Django 开发环境部署(二)python版本控制器pyenv

    python版本控制器pyenv 之前的 那篇是说明了django环境的site package完全独立出来了,但是使用的python解释器还是系统的,为了继续独立出来,甚至是达到ruby的rvm的自 ...

  6. 200 from memory cache / from disk cache / 304 Not Modified 区别

    三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...

  7. 影响Python行为的环境变量

    目录 影响Python行为的环境变量 环境变量 1. PYTHONHOME 2. PYTHONPATH 3. PYTHONSTARTUP 4. PYTHONOPTIMIZE 5. PYTHONBREA ...

  8. 从 posix_spawn() 函数窥探漏洞逃逸

    posix_spawn() 函数是用来在Linux上创建子进程的,头文件是 #include <spawn.h> ,语法如下: #include <spawn.h> int p ...

  9. python: indentationerror: unexpected indent

    以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...

随机推荐

  1. Ajax,学习教程!

    一.什么是Ajax? 2005 年Jesse James Garrett 发表了一篇文章,标题为:“Ajax:A new Approach to Web Applications”.他在这篇文章里介绍 ...

  2. 20160419—JS备忘:服务器回发刷新页面提示重试的解决方案。

    有事页面刷新时 提示如下: js使用的是:location.reload()的刷新方式. 使用js重新定向该页面:location.href="a.aspx"; 当使用:self. ...

  3. [Udemy] ES 7 and Elastic Stack - part 2

    Section 3: Searching with Elasticsearch query with json 分页返回 Sort full text 的内容不能用来sort, 比如movie的 ti ...

  4. nginx-->基本使用

    Nginx基本使用   一.下载 http://nginx.org/en/download.html 二.解压文件 在当前文件夹下通过终端就可以操作nginx nginx -v 三.配置详解 #use ...

  5. 前端 CSS的继承性

    css有两大特性:继承性和层叠性 继承性 继承:给父级设置一些属性,子级继承了父级的该属性,这就是我们的css中的继承. 记住:有一些属性是可以继承下来 : color . font-*. text- ...

  6. css的继承之width属性(容易忽略)

    众所周知,css的三大特性分别是 继承性,层叠性,和优先级. 那么这里就详细说一下css中width的继承性及其特殊情况. 继承性概念详解:css的继承性指的被包在内部的标签拥有外部标签的样式性,子元 ...

  7. Nginx负载均衡的max_fails和fail_timeout的默认配置问题

    今天发现一个奇怪的现象,前端请求后端服务多次后会超时一次,经过多次验证确定是大概10s左右就会超时一次,检查后端服务,发现其中一个节点已经夯死. 但是我们的nginx负载均衡策略是轮询机制,按照配置来 ...

  8. Codeforces 1159E Permutation recovery(构造+拓扑)

    这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了.首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和  3 2 4 5 ...

  9. 面向切面编程 AOP 和装饰器??

    1.AOP概念:面向切面编程,指扩展功能不修改源代码,将功能代码从业务逻辑代码中分离出来. 主要功能:日志记录,性能统计,安全控制,事务处理,异常处理等等. 主要意图:将日志记录,性能统计,安全控制, ...

  10. Bloxorz I (poj3322) (BFS)

    [题目描述] It's a game about rolling a box to a specific position on a special plane. Precisely, the pla ...