Mutation and Iteration
- avoid mutating a list as you are iterating over it
代码:
def remove_dups(L1,L2):
for e in L1:
if e in L2:
L1.remove(e)
L1=[1,2,4,5]
L2=[2,3,1,6]
remove_dups(L1,L2)
L1
》[2, 4, 5] L2
》[2, 3, 1, 6] L1 is [2,4,5], not [4,5] why?
Python uses an internal counter to keep track of index it is in the loop
mutating changes the list length but Python doesn't update the counter
loop never sees element 2 所以上述代码是有问题的,可改为:
def remove_dups_new(L1,L2):
L1_copy = L1[:]
for e in L1_copy:
if e in L2:
L1.remove(e)
Mutation and Iteration的更多相关文章
- HTML5新特性之Mutation Observer
Mutation Observer(变动观察器)是监视DOM变动的接口.当DOM对象树发生任何变动时,Mutation Observer会得到通知. 要概念上,它很接近事件.可以理解为,当DOM发生变 ...
- Python帮助文档中Iteration iterator iterable 的理解
iteration这个单词,是循环,迭代的意思.也就是说,一次又一次地重复做某件事,叫做iteration.所以很多语言里面,循环的循环变量叫i,就是因为这个iteration. iteration指 ...
- 使用HTML5新特性Mutation Observer实现编辑器的撤销和撤销回退操作
MutationObserver介绍 MutationObserver给开发者们提供了一种能在某个范围内的DOM树发生变化时作出适当反应的能力.该API设计用来替换掉在DOM3事件规范中引入的Mut ...
- epoch和Iteration
做机器学习时遇到epoch和iteration,一开始有点迷惑.不是一个意思吗? epoch可以翻译成"回合".一个epoch内,做一次train+一次test iteration ...
- ecshop循环foreach,iteration,key,index
转载: 最近刚接触ecshop不久,感觉是非常的强大,做商城网站,整个流程都差不多搞好了,就是支付流程要自己完善完善,不过也有不足,文章功能还不够好. 通过几天的应用,总结出了ec模版中foreach ...
- Loadrunner中参数化实战(7)-Unique+Each iteration
参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Unique+Each iteration 设置迭代4次Action 结果如下:
- Loadrunner中参数化实战(1)-Sequential+Each iteration
参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Sequential+Each iteration 设置迭代4次Action 结果如下:
- Leetcode: Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- epoch iteration batchsize
深度学习中经常看到epoch. iteration和batchsize,下面按自己的理解说说这三个的区别: (1)batchsize:批大小.在深度学习中,一般采用SGD训练,即每次训练在训练集中取b ...
随机推荐
- Quick Find
--------------------siwuxie095 Quick Find 这里介绍并查集的一种实现思路:Qui ...
- PCL struct“flann::SearchParams参数错误
最近在使用PCL的KdTreeFLANN的时候报错:error C2079: “pcl::KdTreeFLANN<PointT>::param_radius_”使用未定义的 struct“ ...
- Hyperledger Chaincode启动过程
Chaincode 启动过程 简介 这里讲的 Chaincode 是用户链码(User Chaincode,UCC),对应用开发者来说十分重要,它提供了基于区块链分布式账本的状态处理逻辑,基于它可以开 ...
- JSTL标签之core标签的使用
参考:http://blog.csdn.net/qq_25827845/article/details/53311722 核心标签库的导入 <%@ taglib prefix="c&q ...
- IE8下javascript的时间函数Date()不兼容问题,显示NAN【转】
function parseISO8601(dateStringInRange) { var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/, dat ...
- 跨域问题hbuilder
1.借助jquery-jsonp插件 $.jsonp({ url: url, data: { 'name': usd, 'passwd': pass }, callbackParameter: &qu ...
- Examining Application Startup in ASP.NET 5
By Steve Smith June 23, 2015 ASP.NET 5 differs from previous versions of ASP.NET in many ways. Gone ...
- How to safely shut down a loading UIWebView in viewWillDisappear?
up vote24down votefavorite 24 I have a view containing a UIWebView which is loading a google map (so ...
- CentOS7-扩容挂载磁盘
1.1查看新磁盘,可以看到/dev/sdb是新的未挂载的磁盘: [root@localhost ~]# fdisk -l 1.2硬盘分区 ,进入fdisk模式 进入fdisk模式 [root@loca ...
- HTML <area> 标签区域map标签
1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...