如何从api响应创建列表并删除重复项

时间:2019-06-04 21:15:03

标签: python python-2.7 jython

我想从api json响应中列出一个列表,如jira中的每个票证所示,并删除所有重复项

我可以获取每张票证的值,但无法将其作为列表,并从中删除重复项以进行处理

这是每张票证的api json响应

response = {
    "expand": "renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations",
    "id": "1831845",
    "self": "https://jira.com/login/rest/api/latest/issue/1845",
    "key": "pc-1002",
    "fields": {
                 "customfield_1925": {
                    "self": "https://jira.com/login/rest/api/2/customFieldOption/1056",
                    "value": "windows",
                    "id": "101056"
                }

所以我有这样的脚本:

import requests, json

tick = """jira: pc-1002,pc-1003,pc-1005
env"""
ticks = tick.replace(' ','').split(':')[1].split('\n')[0].split(',')
print(ticks)

for i in ticks:
    url = "https://jira.com/login/rest/api/latest/issue/" + str(i)
    print(url)

    response = requests.request("GET", url, verify=False)
    response = json.loads(response.text)
    resp = response['fields']['customfield_1925']['value']   
    print(resp)

因此它打印所有如下所示的值: 输出:

windows1 Windows2的 Windows1

我希望输出值是唯一的,因为它可能最终会重复。

我想要输出如下

['windows1','windows2']

1 个答案:

答案 0 :(得分:0)

只需将每个响应添加到响应列表,然后使用Python方便的“ in”运算符检查每个响应是否已在列表中。类似于:

    allResponses = []
    for i in ticks:
        url = "https://jira.com/login/rest/api/latest/issue/" + str(i)
        print(url)

        response = requests.request("GET", url, verify=False)
        response = json.loads(response.text)
        resp = response['fields']['customfield_1925']['value']
        if resp not in allResponses:   
            print(resp)
            allResponses.append(resp)