如何排序字典列表

时间:2013-06-19 07:21:56

标签: python

我试图根据updatedAt值对Dict列表进行排序,我尝试了以下内容:

from pprint import pprint

abc = [{'couponVote': 100, u'couponTitle': u'15% off on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:48.726Z', u'id': 187, u'createdAt': u'2013-05-31T12:19:48.372Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'74.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'ATRC15', u'couponDescription': u'Flat 15% discount on minimum bill value of Rs. 350. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'15% off on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:49.407Z', u'id': 188, u'createdAt': u'2013-05-31T12:19:49.060Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'69.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'NET03', u'couponDescription': u'15% discount on a minimum bill value of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'20% off on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:50.081Z', u'id': 189, u'createdAt': u'2013-05-31T12:19:49.750Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'76.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'EDM20', u'couponDescription': u'20% off on your order on minimum purchase of Rs. 350 (net price). Not valid on Simply Veg, Simply Non-Veg Pizzas, Combos and Beverages. Valid on online orders only. Offer valid till 31st October 2012.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'20% off on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:50.781Z', u'id': 190, u'createdAt': u'2013-05-31T12:19:50.419Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'77.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'GOIB07', u'couponDescription': u'20% discount on a minimum bill value of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'20% off on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:51.528Z', u'id': 191, u'createdAt': u'2013-05-31T12:19:51.151Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'80.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'NET07', u'couponDescription': u'20% discount on a minimum bill value of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'25% off on Rs. 350 (mobile order)', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:52.204Z', u'id': 192, u'createdAt': u'2013-05-31T12:19:51.863Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'81.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'MOBAPP25', u'couponDescription': u'25% off on minimum order of Rs. 350 from your mobile app. Point your mobile at "http://apps.dominos.co.in/sm/" to download the app. Offer valid till 30th June 2012. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'30% off on Rs. 400', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:52.860Z', u'id': 193, u'createdAt': u'2013-05-31T12:19:52.529Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'83.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'net13', u'couponDescription': u'30% discount on minimum bill value of Rs. 400. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'50% off on second pizza', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:53.561Z', u'id': 194, u'createdAt': u'2013-05-31T12:19:53.197Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'82.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'ADV07', u'couponDescription': u'Get 50% discount on the second pizza. Second pizza should not be of higher value than the first. Not valid on simply veg and simply n-veg pizzas. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'BUY 1 PIZZA GET 1 FREE (mobile app)', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:54.276Z', u'id': 195, u'createdAt': u'2013-05-31T12:19:53.931Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'74.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'DH100', u'couponDescription': u"Buy 1 pizza Get 1 free when you use the Domino's Android app to order. Not valid on Regular size, Simply Veg Pizzas, Simply non-veg Pizzas and combos. Valid for orders from the mobile app ONLY.", u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'free cheesy dip with selected pizza', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:54.966Z', u'id': 196, u'createdAt': u'2013-05-31T12:19:54.620Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'83.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'CRM09', u'couponDescription': u'Get free cheesy dip when you order medium or large pizza. Not valid on simply veg, simply n-veg, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'free choco lava cake on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:55.629Z', u'id': 197, u'createdAt': u'2013-05-31T12:19:55.297Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'79.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'PAYTMSWT', u'couponDescription': u'Free choco lava cake on a minimum bill of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'free choco lava cake on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:56.306Z', u'id': 198, u'createdAt': u'2013-05-31T12:19:55.965Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'81.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'NET02', u'couponDescription': u'Free choco lava cake on a minimum bill of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only. Code 1 of 2.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'free coke on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:57.043Z', u'id': 199, u'createdAt': u'2013-05-31T12:19:56.666Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'72.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'NET04', u'couponDescription': u'Free coke on a minimum bill of Rs. 350. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'Free garlic bread', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:57.697Z', u'id': 200, u'createdAt': u'2013-05-31T12:19:57.366Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'84.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'DH250', u'couponDescription': u'Free Garlic bread and dip with a medium pizza. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'free white pasta veg on Rs. 350', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:58.353Z', u'id': 201, u'createdAt': u'2013-05-31T12:19:58.033Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'80.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'NET01', u'couponDescription': u'Get a free White Pasta Italiano Veg on a minimum purchase of Rs. 350. Not valid on simply veg, simply n-veg pizzas, combos and beverages. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'GENERATE YOUR OWN COUPON', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:59.053Z', u'id': 202, u'createdAt': u'2013-05-31T12:19:58.689Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'83.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'[YOUR OWN COUPON]', u'couponDescription': u"Play Domino's slot machine game (for free) and be given coupon codes when you win. Valid for online order only.", u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'Rs. 100 off on Rs. 500', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:19:59.744Z', u'id': 203, u'createdAt': u'2013-05-31T12:19:59.403Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'79.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'RINW100', u'couponDescription': u'Rs. 100 discount on minimum order of Rs. 500. Valid for online order only.', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'buy1 pizza get 1pizza free', 'commentCount': 0, u'updatedAt': u'2013-05-31T12:20:00.398Z', u'id': 204, u'createdAt': u'2013-05-31T12:20:00.068Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'100.00%', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'May 31 2013', u'success': u' ', u'couponCode': u'adv01', u'couponDescription': u'buy1 pizza get 1pizza free', u'failures': u' '}, {'couponVote': 100, u'couponTitle': u'test coupon ', 'commentCount': 0, u'updatedAt': u'2013-06-19T07:06:40.935Z', u'id': 965, u'createdAt': u'2013-06-19T07:06:40.935Z', u'storeImage': u'http://localhost:8000/store/images/dominos.jpg', u'couponStoreURL_1': u'promocodes-in-dominos/s/401558d9be', u'couponStoreURL_3': u'discounts-in-dominos/s/a6704dd633', u'couponStoreURL_2': u'coupons-in-dominos/s/38984a6340', u'couponStore': u'dominos', 'couponCommentList': [], u'couponValue': u'test', u'storeHash_1': u'401558d9be', u'storeHash_3': u'a6704dd633', u'storeHash_2': u'38984a6340', u'flag': u'remote', u'active': True, u'lastTested': u'June 19 2013', u'success': u' 1', u'couponCode': u'test', u'couponDescription': u'Test', u'failures': u'1 '}]

newlist = sorted(abc, key=lambda k: k['updatedAt'])

pprint(newlist)

我想根据最新更新的价值来安排dict列表...最新的意思应该是第一项等等......可以帮助我一些......我试过的是不行的。 ..

1 个答案:

答案 0 :(得分:3)

你改变排序:

newlist = sorted(abc, key=lambda k: k['updatedAt'], reverse=True)

现在,最近的日期先排序而不是最后排序。