Python推導(dǎo)式數(shù)據(jù)處理方式
前言
推導(dǎo)式是一種獨特的數(shù)據(jù)處理方式,可以快速的從一個數(shù)據(jù)序列構(gòu)建另一個新的數(shù)據(jù)序列的結(jié)構(gòu)體。常用的推導(dǎo)式有一下四種:
- 列表推導(dǎo)式
- 元組推導(dǎo)式
- 集合推導(dǎo)式
- 字典推導(dǎo)式
1、列表推導(dǎo)式
# coding:utf-8 # Author:Yang Xiaopeng """ 語法格式 [表達式 for 變量 in 變量] [表達式 for 變量 in 變量 if 條件表達式] 上述格式中 的 表達式中的變量與for變量一致 """ old_list = [1, 2, 3, 4, 5] new_list = [new_list * new_list for new_list in old_list] # yes [1, 4, 9, 16, 25] # new_list = [new_list1 * new_list for new_list in old_list] # NameError: name 'new_list1' is not defined # new_list = [new_list * new_list for new_list2 in old_list] # NameError: name 'new_list' is not defined old_list = [old_list * old_list for old_list in old_list] # yes [1, 4, 9, 16, 25] print(old_list) print(new_list) new_list = [old_list for old_list in old_list if old_list%2 == 1] # yes [1, 9, 25] print(new_list)
2、元組推導(dǎo)式
# coding:utf-8 # Author:Yang Xiaopeng """ 語法格式 (表達式 for 變量 in 變量) (表達式 for 變量 in 變量 if 條件表達式) 上述格式中 的 表達式中的變量與for變量一致 """ old_list = (1, 2, 3, 4, 5) new_list = (new_list * new_list for new_list in old_list) # yes 1_4_9_16_25_ # new_list = [new_list1 * new_list for new_list in old_list] # NameError: name 'new_list1' is not defined # new_list = [new_list * new_list for new_list2 in old_list] # NameError: name 'new_list' is not defined old_list = (old_list * old_list for old_list in old_list) # yes 1_4_9_16_25_ for item in new_list: print(item, end="_") print("") for item in old_list: print(item, end="_") print("")
3、集合推導(dǎo)式
# coding:utf-8 # Time:2022/6/28 20:57 # Author:Yang Xiaopeng """ 語法格式 {表達式 for 變量 in 變量} {表達式 for 變量 in 變量 if 條件表達式} 上述格式中 的 表達式中的變量與for變量一致 """ old_list = {1, 2, 3, 4, 5} new_list = {new_list * new_list for new_list in old_list} # yes {1, 4, 9, 16, 25} # new_list = {new_list1 * new_list for new_list in old_list} # NameError: name 'new_list1' is not defined # new_list = {new_list * new_list for new_list2 in old_list} # NameError: name 'new_list' is not defined old_list = {old_list * old_list for old_list in old_list} # yes {1, 4, 9, 16, 25} print(old_list) print(new_list) new_list = {old_list for old_list in old_list if old_list % 2 == 1} # yes {1, 9, 25} print(new_list)
4、字典推導(dǎo)式
# coding:utf-8 # Author:Yang Xiaopeng """ 語法格式 {key : value for key in 變量} {key : value for key in 變量 if 表達式} """ old_dict = ["Zhang", "Wang", "Yang", "Jim"] new_dict = {key:len(key) for key in old_dict} # yes {1, 4, 9, 16, 25} print(old_dict) print(new_dict) new_dict = {lll:len(lll) for lll in old_dict if len(lll) % 2 == 0} # yes {1, 9, 25} print(new_dict)
到此這篇關(guān)于Python推導(dǎo)式數(shù)據(jù)處理方式的文章就介紹到這了,更多相關(guān)Python推導(dǎo)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python面向?qū)ο蠡A(chǔ)之常用魔術(shù)方法
這是我聽老師上課做的筆記,文中有非常詳細的代碼示例及注釋,對新手及其友好,對正在學(xué)習(xí)python的小伙伴們也很有幫助,需要的朋友可以參考下2021-05-05Python hexstring-list-str之間的轉(zhuǎn)換方法
今天小編就為大家分享一篇Python hexstring-list-str之間的轉(zhuǎn)換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python實現(xiàn)多線程/多進程的TCP服務(wù)器
這篇文章主要為大家詳細介紹了Python實現(xiàn)多線程/多進程的TCP服務(wù)器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Python文件基本操作open函數(shù)應(yīng)用與示例詳解
這篇文章主要為大家介紹了Python文件基本操作open函數(shù)應(yīng)用與示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12解決python3 HTMLTestRunner測試報告中文亂碼的問題
今天小編就為大家分享一篇解決python3 HTMLTestRunner測試報告中文亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12python實現(xiàn)自動登錄后臺管理系統(tǒng)
這篇文章主要為大家詳細介紹了python實現(xiàn)自動登錄后臺管理系統(tǒng),并進行后續(xù)操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10