新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中的三種推導(dǎo)式介紹
1、列表推導(dǎo)式,在一個(gè)中括號(hào)里包含一個(gè)表達(dá)式。

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供菏澤網(wǎng)站建設(shè)、菏澤做網(wǎng)站、菏澤網(wǎng)站設(shè)計(jì)、菏澤網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、菏澤企業(yè)網(wǎng)站模板建站服務(wù),十余年菏澤做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
old_list = [0,1,2,3,4,5] new_list = [] for item in old_list: if item % 2 == 0: new_list.append(item) print(new_list)
2、字典推導(dǎo)式,把[]改成了{(lán)},并且組成元素有key和value。
old_student_score_info = {
"Jack": {
"chinese": 87,
"math": 92,
"english": 78
},
"Tom": {
"chinese": 92,
"math": 100,
"english": 89
}
}
new_student_score_info = {}
for name, scores in old_student_score_info.items():
if scores["math"] == 100:
new_student_score_info.setdefault(name, scores)
print(new_student_score_info)3、集合推導(dǎo)式跟使用大括號(hào){},組成元素也只要一個(gè)。
old_list = [0, 0, 0, 1, 2, 3]
new_set = {item for item in old_list}
print(new_set)以上就是python中的三種推導(dǎo)式介紹,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)頁標(biāo)題:創(chuàng)新互聯(lián)Python教程:python中的三種推導(dǎo)式介紹
文章路徑:http://fisionsoft.com.cn/article/dpihjoj.html


咨詢
建站咨詢
