我正在编写我的第一个python程序,使用他们的RESTful API管理Atlassian On Demand中的用户。我调用users / search?username = API来检索返回JSON的用户列表。结果是一个复杂的字典类型列表,如下所示:
[
{
"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
}
]
我多次调用它,因此在我的结果中会出现重复的人。我一直在搜索和阅读,但无法弄清楚如何重复删除此列表。我想出了如何使用lambda函数对此列表进行排序。我意识到我可以对列表进行排序,然后迭代并删除重复项。我认为必须有一个更优雅的解决方案。
谢谢!
答案 0 :(得分:0)
用户名是唯一的,对吧?
是否必须是list
?似乎一个简单的解决方案是将其设为dict
dict
。使用用户名作为键,仅存在最新版本。
如果必须订购价值,您可以查看OrderedDict
类型:http://docs.python.org/2/library/collections.html#collections.OrderedDict
答案 1 :(得分:0)
让我们说这就是你所拥有的,
JSON = [
{
"name": "fred",
...
},
{
"name": "peter",
...
},
{
"name": "fred",
...
},
将此dict列表转换为dict的dict将删除副本,如下所示:
r = dict([(user['name'], user) for user in JSON])
在r
中,您只能找到一张fred和peter的记录。