基于python中的时间戳排序字典 [英] Sort dictionaries based on timestamp in python

查看:119
本文介绍了基于python中的时间戳排序字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的列表,

[{u'lang': u'en', u'createdAt': 1326158349, u'canonicalUrl': u'https://foursquare.com/item/4f0b920de4b0c4aaa8bc78ec', u'text': u'one of the great restaurant in new haven , excellent food and service', u'user': {u'lastName': u'S.', u'gender': u'male', u'id': u'19483764', u'firstName': u'Ram', u'photo': {u'prefix': u'https://irs0.4sqi.net/img/user/', u'suffix': u'/blank_boy.png'}}, u'todo': {u'count': 0}, u'id': u'4f0b920de4b0c4aaa8bc78ec', u'logView': True, u'likes': {u'count': 2, u'groups': [{u'count': 2, u'items': [], u'type': u'others'}], u'summary': u'2 likes'}}, {u'lang': u'en', u'createdAt': 1305643797, u'canonicalUrl': u'https://foursquare.com/item/4dd28b15227159fef24d285a', u'url': u'http://www.goscoville.com', u'text': u'buffet quality is alright, and it is cheap', u'user': {u'lastName': u'E.', u'gender': u'male', u'id': u'2617284', u'firstName': u'Perry', u'photo': {u'prefix': u'https://irs1.4sqi.net/img/user/', u'suffix': u'/1OWJL2FYLHWGJGZS.jpg'}}, u'todo': {u'count': 0}, u'id': u'4dd28b15227159fef24d285a', u'logView': True, u'likes': {u'count': 1, u'groups': [{u'count': 1, u'items': [], u'type': u'others'}], u'summary': u'1 like'}}, {u'lang': u'en', u'createdAt': 1352054660, u'canonicalUrl': u'https://foursquare.com/item/5096b784e4b079e230cea793', u'text': u"They're not too talented with meat dishes, so come here for the excellent vegetarian meals, which is the majority of the menu.", u'user': {u'lastName': u'P.', u'gender': u'male', u'id': u'5505251', u'firstName': u'Alex', u'photo': {u'prefix': u'https://irs0.4sqi.net/img/user/', u'suffix': u'/5505251-KAWIX0MMDUPYIS2C.jpg'}}, u'todo': {u'count': 0}, u'id': u'5096b784e4b079e230cea793', u'logView': True, u'likes': {u'count': 0, u'groups': []}}, {u'lang': u'en', u'createdAt': 1337213142, u'canonicalUrl': u'https://foursquare.com/item/4fb440d6e4b002d950289ae6', u'text': u'Stay away from the long haired creepy waiter! Also the shrimp with garlic sauce is reportedly delightful.', u'user': {u'lastName': u'G.', u'gender': u'female', u'id': u'28141671', u'firstName': u'Molly', u'photo': {u'prefix': u'https://irs2.4sqi.net/img/user/', u'suffix': u'/blank_girl.png'}}, u'todo': {u'count': 0}, u'id': u'4fb440d6e4b002d950289ae6', u'logView': True, u'likes': {u'count': 0, u'groups': []}}, {u'lang': u'en', u'createdAt': 1329162806, u'canonicalUrl': u'https://foursquare.com/item/4f396a36e4b0653d4cc83fad', u'text': u'Try the Pho with Chicken (via @Foodspotting)', u'user': {u'lastName': u'c.', u'gender': u'male', u'id': u'3893645', u'firstName': u'louis', u'photo': {u'prefix': u'https://irs1.4sqi.net/img/user/', u'suffix': u'/RWFVP1ETQT0KWQRE.jpg'}}, u'todo': {u'count': 1}, u'id': u'4f396a36e4b0653d4cc83fad', u'logView': True, u'likes': {u'count': 0, u'groups': []}}]

如何根据 timestamp对其元素进行排序? [createdAt key of dict]

推荐答案

使用 list.sort (inplace sorting)或 (返回新的排序列表)并通过功能。键功能的返回值用于比较:

Use list.sort (inplace sorting) or sorted (returns new sorted list) and pass key function. The return value of the key function is used for comparison:

lst.sort(key=lambda d: d['createdAt'])

这篇关于基于python中的时间戳排序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆