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

Python按照l(shuí)ist dict key進(jìn)行排序過(guò)程解析

 更新時(shí)間:2020年04月04日 12:41:18   作者:青春叛逆者  
這篇文章主要介紹了Python按照l(shuí)ist dict key進(jìn)行排序過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在做項(xiàng)目的時(shí)候,遇到這樣的數(shù)據(jù):

"trends": [
        {
          "name": "Rick Gates",
          "promoted_content": null,
          "query": "%22Rick+Gates%22",
          "tweet_volume": 135732,
          "url": "http://twitter.com/search?q=%22Rick+Gates%22"
        },
        {
          "name": "#TheBachelorette",
          "promoted_content": null,
          "query": "%23TheBachelorette",
          "tweet_volume": 91245,
          "url": "http://twitter.com/search?q=%23TheBachelorette"
        },
        {
          "name": "#KremlinAnnex",
          "promoted_content": null,
          "query": "%23KremlinAnnex",
          "tweet_volume": 42654,
          "url": "http://twitter.com/search?q=%23KremlinAnnex"
        },
        {
          "name": "#LHHH",
          "promoted_content": null,
          "query": "%23LHHH",
          "tweet_volume": 35252,
          "url": "http://twitter.com/search?q=%23LHHH"
        }]

我需要做的就是根據(jù)tweet_volume的數(shù)值對(duì)trends里的元素進(jìn)行排序。

實(shí)現(xiàn)代碼:

把上面數(shù)據(jù)以字典的方式獲取,相當(dāng)于把取出的就是后面的列表,即

trends=[
        {
          "name": "Rick Gates",
          "promoted_content": null,
          "query": "%22Rick+Gates%22",
          "tweet_volume": 135732,
          "url": "http://twitter.com/search?q=%22Rick+Gates%22"
        },
        {
          "name": "#TheBachelorette",
          "promoted_content": null,
          "query": "%23TheBachelorette",
          "tweet_volume": 91245,
          "url": "http://twitter.com/search?q=%23TheBachelorette"
        },
        {
          "name": "#KremlinAnnex",
          "promoted_content": null,
          "query": "%23KremlinAnnex",
          "tweet_volume": 42654,
          "url": "http://twitter.com/search?q=%23KremlinAnnex"
        },
        {
          "name": "#LHHH",
          "promoted_content": null,
          "query": "%23LHHH",
          "tweet_volume": 35252,
          "url": "http://twitter.com/search?q=%23LHHH"
        }]

trends = sorted(trends,key = lambda e:e['tweet_volume'],reverse = True)

考慮到有些數(shù)據(jù)是NULL,因此需要提前做個(gè)處理,對(duì)于空的tweet_volume設(shè)置為0,完整代碼:

for item in trends:
  if(item.get('tweet_volume') is None):
    item['tweet_volume'] = 0
  trends = sorted(trends,key = lambda e:.get('tweet_volume') ,reverse = True)

建議用get方式獲取,空值或數(shù)據(jù)不存在這樣不會(huì)報(bào)錯(cuò)。

在Python文檔中看到一種性能更高的方法

通過(guò)使用 operator 模塊的 itemgetter 函數(shù),可以非常容易的排序這樣的數(shù)據(jù)結(jié)構(gòu)

因此上面的程序可以改寫成

from operator import itemgetter
for item in trends:
  if(item.get('tweet_volume') is None):
    item['tweet_volume'] = 0
trends = sorted(trends,key = itemgetter('tweet_volume'),reverse = True)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python排序算法之歸并排序

    python排序算法之歸并排序

    這篇文章主要介紹了python排序算法之歸并排序,歸并排序算法就是一個(gè)先把數(shù)列拆分為子數(shù)列,對(duì)子數(shù)列進(jìn)行排序后,再把有序的子數(shù)列合并為完整的有序數(shù)列的算法,需要的朋友可以參考下
    2023-04-04
  • Django 查詢數(shù)據(jù)庫(kù)返回JSON的實(shí)現(xiàn)

    Django 查詢數(shù)據(jù)庫(kù)返回JSON的實(shí)現(xiàn)

    和前端交互全部使用JSON,如何將數(shù)據(jù)庫(kù)查詢結(jié)果轉(zhuǎn)換成JSON格式,本文就來(lái)介紹一下,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Python快速?gòu)淖⑨屔晌臋n的方法

    Python快速?gòu)淖⑨屔晌臋n的方法

    這篇文章主要介紹了Python快速?gòu)淖⑨屔晌臋n的方法的相關(guān)資料,非常的簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2016-12-12
  • 深入解析Python中的lambda表達(dá)式的用法

    深入解析Python中的lambda表達(dá)式的用法

    這篇文章主要介紹了深入解析Python中的lambda表達(dá)式的用法,包括其與def之間的區(qū)別,需要的朋友可以參考下
    2015-08-08
  • Python中re模塊的常用方法總結(jié)

    Python中re模塊的常用方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Python中re模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • python 檢測(cè)圖片是否有馬賽克

    python 檢測(cè)圖片是否有馬賽克

    這篇文章主要介紹了python 如何檢測(cè)圖片是否有馬賽克,幫助大家更好的理解和使用python處理圖片,感興趣的朋友可以了解下
    2020-12-12
  • python之a(chǎn)rray賦值技巧分享

    python之a(chǎn)rray賦值技巧分享

    今天小編就為大家分享一篇python之a(chǎn)rray賦值技巧分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python的輸入,輸出和標(biāo)識(shí)符詳解

    Python的輸入,輸出和標(biāo)識(shí)符詳解

    這篇文章主要介紹了介紹了Python的輸入,輸出和標(biāo)識(shí)符,有需要的朋友們可以參考一下,希望能給您帶來(lái)幫助
    2022-01-01
  • 一文詳解Python中PO模式的設(shè)計(jì)與實(shí)現(xiàn)

    一文詳解Python中PO模式的設(shè)計(jì)與實(shí)現(xiàn)

    在使用 Python 進(jìn)行編碼的時(shí)候,會(huì)使用自身自帶的編碼設(shè)計(jì)格式,比如說(shuō)最常見的單例模式等。本文將為大家介紹PageObject自動(dòng)化設(shè)計(jì)模式(PO模式)的設(shè)計(jì)與實(shí)現(xiàn),感興趣的可以了解一下
    2022-06-06
  • python中尾遞歸用法實(shí)例詳解

    python中尾遞歸用法實(shí)例詳解

    這篇文章主要介紹了python中尾遞歸用法,較為詳細(xì)的分析了尾遞歸原理與相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論

巴彦县| 武平县| 大新县| 乾安县| 安吉县| 开阳县| 昌黎县| 桃园市| 绥中县| 镶黄旗| 香格里拉县| 临汾市| 库车县| 刚察县| 西华县| 上虞市| 广河县| 瑞丽市| 石狮市| 筠连县| 高平市| 鱼台县| 明水县| 大关县| 会泽县| 聂拉木县| 垫江县| 满洲里市| 张掖市| 衡山县| 嘉善县| 荣成市| 忻城县| 延边| 康保县| 新郑市| 铅山县| 焦作市| 扬中市| 肥乡县| 沾益县|