Python按照list dict key进行排序过程解析
作者:青春叛逆者 发布时间:2023-12-06 08:19:06
标签: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://www.cnblogs.com/liangliangzz/p/12625934.html


猜你喜欢
- 前言pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。但目前pymysql支持python3.x而后者不
- 一、前言最近新买了一台 LG Gram,电脑空荡荡的,啥都得重头装,记录一下 Git 的安装过程,温习温习。二、Git 的安装2.1 Git
- 代码示例#输入'''order_id:31489join_course[0][join_tel]:131309998
- 目录创建django项目使用模型的url.py加载静态文件页面跳转创建数据库模型提交表单提交ajax提交创建django项目创建项目的命令行
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 15 - SlidersMooTools 1.2的
- 相比于逻辑回归,在很多情况下,SVM算法能够对数据计算从而产生更好的精度。而传统的SVM只能适用于二分类操作,不过却可以通过核技巧(核函数)
- 节点类型主要有三种:元素节点,属性节点和文本节点。而对DOM的主要也就是围绕元素节点和属性节点的增删改查。下面就分别从对元素节点的操作和对属
- MySQL主从配置及原理,供大家参考,具体内容如下一、环境选择:1.Centos 6.52.MySQL 5.7二、什么是MySQL主从复制M
- 正则表达式,就是用某种模式去匹配一类字符串的一个公式,正则表达式由一些普通字符和一些元字符(metacharacters)组成。普通字符包括
- 本文实例讲述了Python数据结构与算法之字典树实现方法。分享给大家供大家参考,具体如下:class TrieTree(): d
- 如下所示:import time首先导入时间模块在程序开始执行的地方写入:start=time.clock()在程序末尾写入:end=tim
- 目录前言yarn create 做了什么源码解析项目依赖模版配置工具函数copycopyDiremptyDir核心函数命令行交互并创建文件夹
- 我们先来看一个例子:#encoding=utf-8 # #by panda #桥接模式 def printInfo(info): &nbs
- 目录1 Python变量概述2 Python变量的命名3 Python变量赋值3.1 Python赋值概述3.2 Python变量的基本格式
- 一、单表查询—>更新UPDATE table_nameSET field1=new-value1, field2=new-value2
- 如下所示:import jsonimport http.clientconnection = http.client.HTTPSConnec
- 本节为读者讲解如何利用ADO.NET本身的参数对象和存储过程技术防止注入攻击,以达到用户界面输入与原始SQL的分离,使黑客无法拼接SQL语句
- 引用body标签有两做法: 第一种:使用DOM Core 即引用某个给定文档的第一个(也是仅有的一个)body标签 document.get
- 今天在做sql Server 2005的实验的时候碰到的问题,问题描述很清楚,怀疑是我以前给计算机修改了名称而导致的.可以用select @
- 介绍公司以前的一个exe包,我们需要查看里面python源码,但是以前的py源码文件找不到,所以只能反编译,介绍一下反编译的过程。首先准备: