最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python推導(dǎo)式數(shù)據(jù)處理方式

 更新時(shí)間:2022年07月12日 11:42:08   作者:匿名V5程序員???????  
這篇文章主要介紹了Python推導(dǎo)式數(shù)據(jù)處理方式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下

前言

推導(dǎo)式是一種獨(dú)特的數(shù)據(jù)處理方式,可以快速的從一個(gè)數(shù)據(jù)序列構(gòu)建另一個(gè)新的數(shù)據(jù)序列的結(jié)構(gòu)體。常用的推導(dǎo)式有一下四種:

  • 列表推導(dǎo)式
  • 元組推導(dǎo)式
  • 集合推導(dǎo)式
  • 字典推導(dǎo)式

1、列表推導(dǎo)式

# coding:utf-8
# Author:Yang Xiaopeng

"""
語法格式
[表達(dá)式 for 變量 in 變量]
[表達(dá)式 for 變量 in 變量 if 條件表達(dá)式]
上述格式中 的 表達(dá)式中的變量與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

"""
語法格式
(表達(dá)式 for 變量 in 變量)
(表達(dá)式 for 變量 in 變量 if 條件表達(dá)式)
上述格式中 的 表達(dá)式中的變量與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
"""
語法格式
{表達(dá)式 for 變量 in 變量}
{表達(dá)式 for 變量 in 變量 if 條件表達(dá)式}
上述格式中 的 表達(dá)式中的變量與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 表達(dá)式}
"""
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用python實(shí)現(xiàn)鏈表操作

    使用python實(shí)現(xiàn)鏈表操作

    鏈表是計(jì)算機(jī)科學(xué)里面應(yīng)用最廣泛的數(shù)據(jù)結(jié)構(gòu)之一。這篇文章主要介紹了使用python實(shí)現(xiàn)鏈表操作,需要的朋友可以參考下
    2018-01-01
  • 詳解pandas.DataFrame中刪除包涵特定字符串所在的行

    詳解pandas.DataFrame中刪除包涵特定字符串所在的行

    這篇文章主要介紹了pandas.DataFrame中刪除包涵特定字符串所在的行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python代碼中如何讀取鍵盤錄入的值

    Python代碼中如何讀取鍵盤錄入的值

    在本篇文章里小編給大家分享的是關(guān)于Python代碼中讀取鍵盤錄入值的方法,需要的朋友們可以參考下。
    2020-05-05
  • Python 多進(jìn)程、多線程效率對(duì)比

    Python 多進(jìn)程、多線程效率對(duì)比

    這篇文章主要介紹了Python 多進(jìn)程、多線程的效率對(duì)比,幫助大家選擇適合的技術(shù),感興趣的朋友可以了解下
    2020-11-11
  • Python 解決logging功能使用過程中遇到的一個(gè)問題

    Python 解決logging功能使用過程中遇到的一個(gè)問題

    這篇文章主要介紹了Python 解決logging功能使用過程中遇到的一個(gè)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Flask深入了解Jinja2引擎的用法

    Flask深入了解Jinja2引擎的用法

    Jinja2是基于python的模板引擎,功能比較類似于于PHP的smarty,J2ee的Freemarker和velocity。 它能完全支持unicode,并具有集成的沙箱執(zhí)行環(huán)境,應(yīng)用廣泛。jinja2使用BSD授權(quán)
    2022-07-07
  • Python實(shí)現(xiàn)讀取Linux系統(tǒng)的CPU以及內(nèi)存占用

    Python實(shí)現(xiàn)讀取Linux系統(tǒng)的CPU以及內(nèi)存占用

    這篇文章主要為大家詳細(xì)介紹了如何利用Python語言實(shí)現(xiàn)Linux系統(tǒng)的CPU以及內(nèi)存占用,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以收藏一下
    2023-05-05
  • Pandas之排序函數(shù)sort_values()的實(shí)現(xiàn)

    Pandas之排序函數(shù)sort_values()的實(shí)現(xiàn)

    這篇文章主要介紹了Pandas之排序函數(shù)sort_values()的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python操作docx寫入內(nèi)容,并控制文本的字體顏色

    python操作docx寫入內(nèi)容,并控制文本的字體顏色

    今天小編就為大家分享一篇python操作docx寫入內(nèi)容,并控制文本的字體顏色,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • opencv檢測(cè)動(dòng)態(tài)物體的實(shí)現(xiàn)

    opencv檢測(cè)動(dòng)態(tài)物體的實(shí)現(xiàn)

    本文主要介紹了opencv檢測(cè)動(dòng)態(tài)物體的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07

最新評(píng)論

乌兰察布市| 青浦区| 黄浦区| 崇义县| 榆林市| 明水县| 福建省| 公安县| 商水县| 青州市| 永胜县| 安岳县| 湘乡市| 新化县| 平舆县| 衡阳县| 博白县| 深圳市| 富宁县| 竹山县| 定南县| 滦平县| 精河县| 平乐县| 闽清县| 长武县| 新干县| 海原县| 衡山县| 富裕县| 扎赉特旗| 洮南市| 洛南县| 沈丘县| 大埔区| 安乡县| 西城区| 那曲县| 邹平县| 清涧县| 孝义市|