按特定顺序排列字典列表 [英] Sorting list of dictionaries according to specific order

查看:234
本文介绍了按特定顺序排列字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Python 2.6,我有两个数据存储。查询第一个按特定顺序返回文档ID列表。我使用这些ID在第二个数据存储中查找所有文档,它们返回一个字典列表(每个文档一个),但不是与原始列表相同的顺序。我现在需要重新排序这个字典列表,以便这些文件的顺序是他们的ID在第一个列表中。这样做最好的方法是什么?

解决方案

最好的(和一般的)解决方案似乎在这里给出:在python中任意排列的列表


I am using Python 2.6 and I have two data stores. Querying the first one returns a list of document IDs in a specific order. I look up all the documents at once in the second data store using these IDs, which returns a list of dictionaries (one for each doc), but not in the same order as the original list. I now need to re-sort this list of dictionaries so that the documents are in the order that their IDs were in the first list. What's the best way of doing this?

解决方案

The best (and general) solution seems to be given here: reordering list of dicts arbitrarily in python

这篇关于按特定顺序排列字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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