【问题标题】:Sort dictionaries based on timestamp in python根据python中的时间戳对字典进行排序
【发布时间】:2014-05-01 19:02:20
【问题描述】:

这是我的清单,

[{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] 对其元素进行排序?

【问题讨论】:

    标签: python list sorting python-3.x dictionary


    【解决方案1】:

    使用list.sort(就地排序)或sorted(返回新的排序列表)并传递key函数。 key函数的返回值用于比较:

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

    【讨论】:

      【解决方案2】:

      您可以将operator.itemgetter 用作sortedlist.sort 的键:

      from operator import itemgetter
      my_list.sort(key=itemgetter('createdAt'))
      

      【讨论】:

        猜你喜欢
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 2018-08-09
        相关资源
        最近更新 更多