简体   繁体   English

从嵌套词典列表中删除重复项

[英]Remove duplicates from a list of nested dictionaries

I'm writing my first python program to manage users in Atlassian On Demand using their RESTful API. 我正在编写我的第一个python程序,以使用他们的RESTful API管理Atlassian On Demand中的用户。 I call the users/search?username= API to retrieve lists of users, which returns JSON. 我调用users / search?username = API来检索用户列表,该列表返回JSON。 The results is a list of complex dictionary types that look something like this: 结果是一列复杂的字典类型,看起来像这样:

[
        {
            "self": "http://www.example.com/jira/rest/api/2/user?username=fred",
            "name": "fred",
            "avatarUrls": {
                "24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred",
                "16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred",
                "32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred",
                "48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred"
            },
            "displayName": "Fred F. User",
            "active": false
        },
        {
            "self": "http://www.example.com/jira/rest/api/2/user?username=andrew",
            "name": "andrew",
            "avatarUrls": {
                "24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=andrew",
                "16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=andrew",
                "32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=andrew",
                "48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=andrew"
            },
            "displayName": "Andrew Anderson",
            "active": false
        }
    ]

I'm calling this multiple times and thus getting duplicate people in my results. 我多次打电话给我,结果得到了重复的人。 I have been searching and reading but cannot figure out how to deduplicate this list. 我一直在搜索和阅读,但无法弄清楚如何对该列表进行重复数据删除。 I figured out how to sort this list using a lambda function. 我想出了如何使用lambda函数对该列表进行排序。 I realize I could sort the list, then iterate and delete duplicates. 我意识到我可以对列表进行排序,然后迭代并删除重复项。 I'm thinking there must be a more elegant solution. 我认为必须有一个更优雅的解决方案。

Thank you! 谢谢!

The usernames are unique, right? 用户名是唯一的,对吗?

Does it have to be a list ? 它一定是list吗? Seems like an easy solution would be to make it a dict of dict s instead. 似乎是一个简单的解决办法是使它成为一个dictdict !而非。 Use the usernames as keys, and only the most recent version will be present. 使用用户名作为密钥,将仅显示最新版本。

If the values have to be ordered, there is an OrderedDict type you could look into: http://docs.python.org/2/library/collections.html#collections.OrderedDict 如果必须对值进行排序,则可以查看一个OrderedDict类型: http : //docs.python.org/2/library/collections.html#collections.OrderedDict

Let say it is what you got, 可以说这就是你得到的,

JSON = [
        {

            "name": "fred",
...
},
        {

            "name": "peter",
...
},
        {

            "name": "fred",
...
},

Convert this list of dict to a dict of dict will remove the duplicate, like so: 将此字典列表转换为字典,将删除重复的字典,如下所示:

r = dict([(user['name'], user) for user in JSON])

In r you will only find one record of fred and peter each. r您只会找到fred和peter的一个记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM