99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python中的復(fù)制操作及copy模塊中的淺拷貝與深拷貝方法
Python中的復(fù)制操作及copy模塊中的淺拷貝與深拷貝方法
2018-01-05
收藏

Python中的復(fù)制操作及copy模塊中的淺拷貝與深拷貝方法

淺拷貝和深拷貝是Python基礎(chǔ)學(xué)習(xí)中必須辨析的知識(shí)點(diǎn),這里我們將為大家解析Python中的復(fù)制操作及copy模塊中的淺拷貝與深拷貝方法:

程序中常常需要復(fù)制一個(gè)對(duì)象, 按思路應(yīng)該是這樣的    
a = [1, 2, 3]
b = a
 
# [1, 2, 3]
print b

已經(jīng)復(fù)制好了,但是現(xiàn)在得改變一下第一個(gè)元素的值把它改成5    
b[0] = 5
 
# [5, 2, 3]
print b
 
# [5, 2, 3]
print a

我改變了b的第一個(gè)元素的值,但是a的值也改變了,這是因?yàn)?a href='/map/python/' style='color:#000;font-size:inherit;'>python中的=是引用.a和b指向的是相同的列表,所以改變列表會(huì)出現(xiàn)以上的結(jié)果.
解決方法是切片操作    
a = [1, 2, 3]
b = a[:]
b[0] = 4
 
# [1, 2, 3]
# [4, 2, 3]
print a
print b

但是在嵌套列表的時(shí)候呢,試一試    
a = [[1,2,3], 4, 5]
b = a[:]
b[1] = 0
 
# [[1,2,3], 4, 5]
# [[1,2,3], 0, 5]
print a
print b

恩!沒(méi)什么問(wèn)題,在試一試嵌套列表元素    
a = [[1,2,3], 4, 5]
b = a[:]
b[0][0] = 5
 
# [[5,2,3], 4, 5]
# [[5,2,3], 4, 5]
print a
print b
b = a[:]

a的值還是改變了,切片復(fù)制只對(duì)該對(duì)象進(jìn)行拷貝不會(huì)對(duì)子元素進(jìn)行拷貝

copy 模塊

copy模塊用于對(duì)象的拷貝操作。該模塊非常簡(jiǎn)單,只提供了兩個(gè)主要的方法: copy.copy 與 copy.deepcopy ,分別表示淺復(fù)制與深復(fù)制。什么是淺復(fù)制,什么是深復(fù)制,網(wǎng)上有一卡車一卡車的資料,這里不作詳細(xì)介紹。復(fù)制操作只對(duì)復(fù)合對(duì)象有效。用簡(jiǎn)單的例子來(lái)分別介紹這兩個(gè)方法。

淺復(fù)制只復(fù)制對(duì)象本身,沒(méi)有復(fù)制該對(duì)象所引用的對(duì)象。    
#coding=gbk
import copy
l1 = [1, 2, [3, 4]]
l2 = copy.copy(l1)
print l1
print l2
l2[2][0] = 50
print l1
print l2

結(jié)果:    
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [50, 4]]
[1, 2, [50, 4]]

同樣的代碼,使用深復(fù)制,結(jié)果就不一樣:    
import copy
l1 = [1, 2, [3, 4]]
l2 = copy.deepcopy(l1)
print l1
print l2
l2[2][0] = 50
print l1
print l2

結(jié)果:    
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [3, 4]]
[1, 2, [50, 4]]

改變copy的默認(rèn)行為

在定義類的時(shí)候,通過(guò)定義__copy__和__deepcopy__方法,可以改變copy的默認(rèn)行為。下面是一個(gè)簡(jiǎn)單的例子:    
class CopyObj(object):
  def __repr__(self):
    return "CopyObj"
   
  def __copy__(self):
    return "Hello"
obj = CopyObj()
obj1 = copy.copy(obj)
print obj
print obj1

結(jié)果:    
CopyObj
Hello


數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }