Python按照list dict key進行排序過程解析
在做項目的時候,遇到這樣的數(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ù)值對trends里的元素進行排序。
實現(xiàn)代碼:
把上面數(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" }] trends = sorted(trends,key = lambda e:e['tweet_volume'],reverse = True)
考慮到有些數(shù)據(jù)是NULL,因此需要提前做個處理,對于空的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ù)不存在這樣不會報錯。
在Python文檔中看到一種性能更高的方法
通過使用 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)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV+Python3.5 簡易手勢識別的實現(xiàn)
這篇文章主要介紹了OpenCV+Python3.5 簡易手勢識別的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Matplotlib scatter繪制散點圖的方法實現(xiàn)
這篇文章主要介紹了Matplotlib scatter繪制散點圖的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01PyCharm 2020.2下配置Anaconda環(huán)境的方法步驟
這篇文章主要介紹了PyCharm 2020.2下配置Anaconda環(huán)境的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09從源碼解析Python的Flask框架中request對象的用法
Flask中的request對象發(fā)送請求使用起來十分方便,但也有一些需要注意的地方,這里我們來從源碼解析Python的Flask框架中request對象的用法,需要的朋友可以參考下.2016-06-06Python獲取當前公網(wǎng)ip并自動斷開寬帶連接實例代碼
這篇文章主要介紹了Python獲取當前公網(wǎng)ip并自動斷開寬帶連接實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01Python NumPy實現(xiàn)數(shù)組搜索示例詳解
NumPy是一個開源的Python科學計算庫,使用NumPy可以很自然地使用數(shù)組和矩陣,這篇文章主要介紹了使用NumPy實現(xiàn)數(shù)組搜索,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習吧2023-05-05Python報錯ValueError:?cannot?convert?float?NaN?to?intege
在Python編程中,我們經(jīng)常需要處理各種數(shù)據(jù)類型,包括浮點數(shù)和整數(shù),然而,有時候我們可能會遇到一些意外的情況,比如將一個包含NaN(Not?a?Number)的浮點數(shù)轉(zhuǎn)換為整數(shù)時,就會拋出錯誤,本文將探討這個錯誤的原因,并給出幾種可能的解決方案,需要的朋友可以參考下2024-09-09