The list of list is modified unexpected, python
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的更多相关文章
- Python实践:模块自动重载
一.概述 二.思路 三.实现 四.测试 1.开启自动重载(终端1) 2.修改模块(终端2) 3.查看实时输出(终端1) 五.参考源码 一.概述 开发Web程序时,通常会采用本地服务器进行调试,但如果代 ...
- Python:渗透测试开源项目
Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...
- Python关键点笔记之使用 pyenv 管理多个 Python 版本依赖环境
0x00 背景 从接触Python以来,一直都是采用virtualenv和virtualenvwrapper来管理不同项目的依赖环境,通过workon.mkvirtualenv等命令进行虚拟环境切换, ...
- Python:渗透测试开源项目【源码值得精读】
sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网 ...
- Linux——Django 开发环境部署(二)python版本控制器pyenv
python版本控制器pyenv 之前的 那篇是说明了django环境的site package完全独立出来了,但是使用的python解释器还是系统的,为了继续独立出来,甚至是达到ruby的rvm的自 ...
- 200 from memory cache / from disk cache / 304 Not Modified 区别
三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...
- 影响Python行为的环境变量
目录 影响Python行为的环境变量 环境变量 1. PYTHONHOME 2. PYTHONPATH 3. PYTHONSTARTUP 4. PYTHONOPTIMIZE 5. PYTHONBREA ...
- 从 posix_spawn() 函数窥探漏洞逃逸
posix_spawn() 函数是用来在Linux上创建子进程的,头文件是 #include <spawn.h> ,语法如下: #include <spawn.h> int p ...
- python: indentationerror: unexpected indent
以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...
随机推荐
- python 每周作业
day2:python的简介与认识day2:# 1.写一个登录程序# username# passwd# 让用户输入账号和密码,输入用户和密码输入正确的话# 提示你 xxx,欢迎登录,今天的日期是xx ...
- python 设置开机启动脚本
1.创建python_auto.bat的快捷方式,放入启动项: C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\ ...
- Windows操作系统命令整理-Win7
注册表(Windows95以后Windows系统版本,windows server中regedit和regedit32合并为一个新的编辑器,名称仍然是regedit) HKEY_LOCAL_MACHI ...
- 006/搭建fabric(二)
准备vmware虚拟机,并安装完ubuntu系统后.继续搭建fabric运行环境... 0.打开终端,切换root身份.目的:后续操作即可不用sudo... 右键->open Terminal- ...
- 四种pop模式介绍
四种pop模式介绍 URL:http://www.hishop.com.cn/ecschool/jd/show_21195.html URL:https://zhidao.baidu.com/ques ...
- Java相关面试题总结+答案(七)
[Hibernate] 113. 为什么要使用 hibernate? hibernate 是对 jdbc 的封装,大大简化了数据访问层的繁琐的重复性代码. hibernate 是一个优秀的 ORM 实 ...
- UVA 12849 Mother’s Jam Puzzle( 高斯消元 )
题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...
- NGUI的HUD Text的扩展插件学习--(HUDText)的使用
一,我们先添加一个空的游戏对象,在菜单中找到这个添加空的游戏对象 二,然后我们给该对象添加HUDText,然后给这个添加字体 三,我们添加个脚本,代码如下: using UnityEngine; us ...
- java 快速开发平台 有代码生成器 springmvc SSM后台框架源码
. 权限管理:点开二级菜单进入三级菜单显示 角色(基础权限)和按钮权限 角色(基础权限): 分角色组和角色,独立分配菜单权限和增删改查权限. 按钮权限: 给角色分配按钮权限.2 ...
- Docker实战部署应用——Tomcat
Tomcat 部署 拉取tomcat镜像 docker pull tomcat:8 创建tomcat容器 创建tomcat容器用于 Web应用,并且进行目录映射 docker run -id --na ...