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

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python字典類(lèi)型專(zhuān)用運(yùn)算符
Python字典類(lèi)型專(zhuān)用運(yùn)算符
2022-06-21
收藏

作者:小K

來(lái)源:麥?zhǔn)寰幊?/span>

Python字典類(lèi)型專(zhuān)用運(yùn)算符

?

如果我們?cè)趯?xiě)Python代碼的時(shí)候需要把多個(gè)字典合并,那么我們將如何使用代碼去實(shí)現(xiàn)呢?

?

回想合并字典的辦法

1. 遍歷轉(zhuǎn)移

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS"}
for k, v in origin_dct.items():
    new_dct[k] =  v

print(new_dct)

代碼運(yùn)行結(jié)果:

{'Garvey': 'CSS', 'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP'}

2. 用dict(b, **a)方法創(chuàng)建一個(gè)新的字典

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS"}
dct = dict(new_dct, **origin_dct)
print(dct)

代碼運(yùn)行結(jié)果:

{'Garvey': 'CSS', 'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP'}

3. 用dict.update(param)方法

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS"}
new_dct.update(origin_dct)
print(new_dct)

代碼運(yùn)行結(jié)果:

{'Garvey': 'CSS', 'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP'}

4. 把字典拆成其他類(lèi)型,合并完后再轉(zhuǎn)成字典

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS"}
dct = dict(list(origin_dct.items()) + list(new_dct.items()))

print(dct)

代碼運(yùn)行結(jié)果:

{'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP', 'Garvey': 'CSS'}
Python字典類(lèi)型專(zhuān)用運(yùn)算符

使用運(yùn)算符“|”合并字典

以上四種方法都是使用函數(shù)進(jìn)行合并的,看上去都不太簡(jiǎn)便。

使用運(yùn)算符合并的話,代碼會(huì)更直觀,對(duì)讀寫(xiě)的人都更友好。

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS"}

dct = origin_dct | new_dct
print(dct)

代碼運(yùn)行結(jié)果:

{'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP', 'Garvey': 'CSS'}

?

使用這種方式合并字典是不是很Python?不著急,合并運(yùn)算符“|”在字典中還有另一種使用方法。

?

使用“|=” 可以用來(lái)更新字典

上面使用合并運(yùn)算符之后,會(huì)生成一個(gè)新的字典對(duì)象。

那么如果我只是單純的想把合并結(jié)果更新到已存在的字典對(duì)象中呢?

類(lèi)似于列表的append方法,或 x += 1操作。

origin_dct = {"Maishu":"python", "Kevin":"java", "Jason":"PHP"}
new_dct = {"Garvey":"CSS","Maishu":"Go"}

origin_dct |= new_dct
print(origin_dct)

代碼運(yùn)行結(jié)果:

{'Maishu': 'python', 'Kevin': 'java', 'Jason': 'PHP', 'Garvey': 'CSS'}

使用|=可以輕松實(shí)現(xiàn)。

后記

還記得|運(yùn)算符在Python的哪個(gè)內(nèi)容中有出現(xiàn)嗎?

| 按位或運(yùn)算符,屬于位運(yùn)算符。

在某些場(chǎng)合|也會(huì)被用來(lái)做“或”邏輯運(yùn)算符,例如在正則中。

當(dāng)然做合并操作的時(shí)候只有在碰到字典類(lèi)型。

Python字典類(lèi)型專(zhuān)用運(yùn)算符

數(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); }