如何在Python中排列列表然后返回?

时间:2019-11-04 13:56:10

标签: python

您好,我有以下python脚本,该脚本从列表和连接中获取有关我在TCC的测试应用程序中导入的javascript的信息。

import json

lista = [
    {
        "ExceptionsAPI": {
            "ExceptionName": "aad_training",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "aad_account",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "AccountStructure",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "philippe",
            "TargetHostname": "empresa2.info",
            "TargetName": "empresa2"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "porto",
            "TargetHostname": "empresa2.info",
            "TargetName": "empresa2"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "obidos",
            "TargetHostname": "empresa3.net",
            "TargetName": "empresa3"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "coimbra",
            "TargetHostname": "empresa3.net",
            "TargetName": "empresa3"
        }
    }
]



    regexp = []
    target = []
    for chaves in lista:
        dicionario = chaves['ExceptionsAPI']
        target.append(dicionario['TargetName'])
        hostname = (dicionario['TargetHostname'])
        regexp.append(dicionario['ExceptionName'])



    regexp = str('|').join(regexp)
    print('\n Targets in the List'.join(target))

    print('\n We have the following exceptions for the company1:', regexp)

    print('\n We have the following domain:', hostname)

输出:

    empresa1
     Targets in Listaempresa2
     Targets in Listaempresa3
    ('\n We have the following exceptions for the company1', 'aad_training|aad_account|coimbra')
    ('\n  We have the following domain:', 'empresa3.net')

它正在工作,但是我需要帮助。我需要您回到每个“ TargetName”这样的组织:

因此,正如您可以验证的那样,对于每个目标名称,都用print书写并向我返回每个'TargetName'的每个'ExecptionName'的正确组织

我需要什么,对于每个目标为1或100的目标,它会返回给我这个组织并自动进行组装。

我需要什么,回报是这样的

    The following targets are listed:

    Targets in the List: company1
    Targets in the List: company2
    Targets in the List: company3



    We have the following exceptions for the company1:
    your domain and: empresa1.org
    their targets are: aad_training;aad_account; AccountStructure, New

    We have the following exceptions for the company2:
    your domain and: empresa2.info
    its targets are: philippe;porto

    We have the following exceptions for the company3:
    your domain and: empresa3.net
    its targets are: obidos;coimbra

1 个答案:

答案 0 :(得分:0)

请使用以下内容自动列出您的要求。

import re
lista = [
    {
        "ExceptionsAPI": {
            "ExceptionName": "aad_training",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "aad_account",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "AccountStructure",
            "TargetHostname": "empresa1.org",
            "TargetName": "empresa1"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "philippe",
            "TargetHostname": "empresa2.info",
            "TargetName": "empresa2"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "porto",
            "TargetHostname": "empresa2.info",
            "TargetName": "empresa2"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "obidos",
            "TargetHostname": "empresa3.net",
            "TargetName": "empresa3"
        }
    },
    {
        "ExceptionsAPI": {
            "ExceptionName": "coimbra",
            "TargetHostname": "empresa3.net",
            "TargetName": "empresa3"
        }
    }
]


companies = set(filter(lambda y: re.match('^[a-zA-Z0-9]+$',y),map(lambda x: x['ExceptionsAPI']['TargetName'],lista)))

print(str('\n').join(set(map(lambda x: f'{x[0]} {x[1]}',zip(['Targets in the List:']*len(companies),companies)))))

for i in sorted(companies):
    print(f'We have the following exceptions for the {i}')
    clist = list(filter(lambda x:x['ExceptionsAPI']['TargetName'] == i,lista))
    hostname = set(map(lambda x:x['ExceptionsAPI']['TargetHostname'],clist))
    targets = set(map(lambda x:x['ExceptionsAPI']['ExceptionName'],clist))
    print(f'your domain and: {",".join(hostname)}')
    print(f'its targets are: {";".join(targets)}')

这将为您提供预期的输出:

Targets in the List: empresa1
Targets in the List: empresa3
Targets in the List: empresa2
We have the following exceptions for the empresa1
your domain and: empresa1.org
its targets are: AccountStructure;aad_training;aad_account
We have the following exceptions for the empresa2
your domain and: empresa2.info
its targets are: philippe;porto
We have the following exceptions for the empresa3
your domain and: empresa3.net
its targets are: obidos;coimbra