对python append 与浅拷贝的实例讲解


Posted in Python onMay 04, 2018

在做Leetcode的第39题的时候,看到网上一个用递归的解法,很简洁。于是重写了一遍。

class Solution(object):
 def combinationSum(self, candidates, target):
 """
 :type candidates: List[int]
 :type target: int
 :rtype: List[List[int]]
 """
 result,temp = [],[]
 self.combinationSumRecu(sorted(candidates),result,0,temp,target)
 return result

 def combinationSumRecu(self, candidates, result, start, temp, target):
 if target == 0:
  result.append(temp) # 注意此处不能直接append(temp),否则是浅拷贝,之后temp.pop()时会将result中的数也pop出来
 while start < len(candidates) and candidates[start]<=target:
  temp.append(candidates[start])
  self.combinationSumRecu(candidates, result, start, temp,target-candidates[start])
  temp.pop()
  start += 1

if __name__ == '__main__':
 print Solution().combinationSum([2,3,6,7],7)

一开始没看懂combinationSumRecu中的result.append(list(temp))为什么temp要加list,因为temp本身就是一个list。但是把list去掉后,结果就出现错误。

没改前,结果是:

[[2, 2, 3], [7]]

改成result.append(temp)后:

[[], []]

为什么会这样呢?list在这里做了什么工作呢?

首先,为了验证temp每步都是一个list,我们是使用type()函数查看它的类型。

if target == 0: 
 print type(temp),temp,result 
 result.append(temp)

输出为:

<type 'list'> [2, 2, 3] []
<type 'list'> [7] [[7]]

可以看出,temp都是list。但是第二个result的结果不正确

可以将正确的值输出对比一下

if target == 0: 
 print type(temp),temp,result 
 result.append(list(temp))

输出为:

<type 'list'> [2, 2, 3] []
<type 'list'> [7] [[7]]

可以看出,本来第二个result应该为[[2,2,3]],结果变成了[[7]].

于是猜想可能是append()浅拷贝问题。

append(temp)后又在后面进行temp.pop()操作。result实际上append的是temp的引用。当temp所指向的地址的值发生改变时,result也会跟着改变。

举个例子验证一下:

a = [1,2] 
b = [3,4] 
a.append(b) 
print a 
b.pop() 
print a

输出结果为:

[1, 2, [3, 4]]
[1, 2, [3]]

要解决这个问题,需要对temp进行深拷贝后append到result中。而list(temp)就会返回temp的一个深拷贝。

除了用list(temp)以外,还可以用temp[:]进行深拷贝。

以上这篇对python append 与浅拷贝的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现探测socket和web服务示例
Mar 28 Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 Python
Python实现控制台输入密码的方法
May 29 Python
python实现12306火车票查询器
Apr 20 Python
Python实现采用进度条实时显示处理进度的方法
Dec 19 Python
Python面向对象基础入门之编码细节与注意事项
Dec 11 Python
python实现邮件发送功能
Aug 10 Python
python多进程(加入进程池)操作常见案例
Oct 21 Python
Tensorflow:转置函数 transpose的使用详解
Feb 11 Python
jupyter notebook 增加kernel教程
Apr 10 Python
从0到1使用python开发一个半自动答题小程序的实现
May 12 Python
Python基础教程(一)——Windows搭建开发Python开发环境
Jul 20 Python
浅谈Python中重载isinstance继承关系的问题
May 04 #Python
对Python 2.7 pandas 中的read_excel详解
May 04 #Python
Python3读取Excel数据存入MySQL的方法
May 04 #Python
详解Django之admin组件的使用和源码剖析
May 04 #Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
May 04 #Python
使用python3+xlrd解析Excel的实例
May 04 #Python
对python中的xlsxwriter库简单分析
May 04 #Python
You might like
第七章 php自定义函数实现代码
2011/12/30 PHP
php简单的留言板与回复功能具体实现
2014/02/19 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
2018/06/20 PHP
PHP attributes()函数讲解
2019/02/03 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
Extjs学习笔记之五 一个小细节renderTo和applyTo的区别
2010/01/07 Javascript
js图片延迟加载的实现方法及思路
2013/07/22 Javascript
JavaScript基本的输出和嵌入式写法教程
2015/10/20 Javascript
jQuery实现的tab标签切换效果示例
2016/09/05 Javascript
详解Angualr 组件间通信
2017/01/21 Javascript
jQuery图片轮播功能实例代码
2017/01/29 Javascript
bootstrap3-dialog-master模态框使用详解
2017/08/22 Javascript
用vue构建多页面应用的示例代码
2017/09/20 Javascript
node实现分片下载的示例代码
2018/10/17 Javascript
JS简单数组排序操作示例【sort方法】
2019/05/17 Javascript
JS用最简单的方法实现四舍五入
2019/08/27 Javascript
解决layui批量传值到后台操作时出现传值为空的问题
2019/09/28 Javascript
JavaScript canvas仿代码流瀑布
2020/02/10 Javascript
python列表操作实例
2015/01/14 Python
Windows下安装python2.7及科学计算套装
2015/03/05 Python
详解Python使用tensorflow入门指南
2018/02/09 Python
详解python中自定义超时异常的几种方法
2019/07/29 Python
python时间日期操作方法实例小结
2020/02/06 Python
深入浅析Python 命令行模块 Click
2020/03/11 Python
jupyter notebook读取/导出文件/图片实例
2020/04/16 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
Python字符串及文本模式方法详解
2020/09/10 Python
Python经纬度坐标转换为距离及角度的实现
2020/11/01 Python
HTML5之WebGL 3D概述(上)—WebGL原生开发开启网页3D渲染新时代
2013/01/31 HTML / CSS
人力资源专员岗位职责
2014/01/30 职场文书
销售冠军获奖感言
2014/02/03 职场文书
超市采购员岗位职责
2015/04/07 职场文书
导游词之杭州岳王庙
2019/11/13 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python
Spring IOC容器Bean的作用域及生命周期实例
2022/05/30 Java/Android
GPU服务器的多用户配置方法
2022/07/07 Servers