Python按照list中dict的某個key進行排序


在做項目的時候,遇到這樣的數據:

 "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"
               }]

我需要做的就是根據tweet_volume的數值對trends里的元素進行排序。
實現代碼:
把上面數據以字典的方式獲取,相當於把取出的就是后面的列表,即
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)
考慮到有些數據是NULL,因此需要提前做個處理,對於空的tweet_volume設置為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方式獲取,空值或數據不存在這樣不會報錯。
在Python文檔中看到一種性能更高的方法

通過使用 operator 模塊的 itemgetter 函數,可以非常容易的排序這樣的數據結構

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


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)

本文參考地址:

https://blog.csdn.net/qq_29303759/article/details/81902810

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM