具有两个循环和两个条件的Python列表理解

问题描述 投票:0回答:1

我有以下数据,我想要做的是将来自task_resources的那些在task_id和qos_level上匹配的字典放入新列表中:

task_resources = [
  {
    "resource_id": "52a58a34-6b1b-49b7-b53d-2bc4bf72c172",
    "domain_name": "Nep",
    "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
    "qos_level": "default",
    "cpu_cores": 1,
    "memory_mb": 128,
    "storage_mb": 0,
    "cpu_flops": 19177,
    "gpu_flops": 0,
    "gpu_memory_mb": 0,
    "egress_network_bandwidth_gbps": 0,
    "ingress_network_bandwidth_gbps": 0,
    "create_time": "2024-11-13T12:13:52.192657"
  },
  {
    "resource_id": "8208915d-90b4-4a6c-a98c-e9eb965b4e6b",
    "domain_name": "Nep",
    "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
    "qos_level": "low",
    "cpu_cores": 1,
    "memory_mb": 128,
    "storage_mb": 0,
    "cpu_flops": 19177,
    "gpu_flops": 0,
    "gpu_memory_mb": 0,
    "egress_network_bandwidth_gbps": 0,
    "ingress_network_bandwidth_gbps": 0,
    "create_time": "2024-11-13T12:13:52.202177"
  },
  {
    "resource_id": "c34a4411-20d3-42eb-9f74-1336ab6ab024",
    "domain_name": "Nep",
    "tasks_id": "95bc75a6-b292-484d-a65d-86fae2cb6b6f",
    "qos_level": "default",
    "cpu_cores": 1,
    "memory_mb": 128,
    "storage_mb": 0,
    "cpu_flops": 19177,
    "gpu_flops": 0,
    "gpu_memory_mb": 0,
    "egress_network_bandwidth_gbps": 0,
    "ingress_network_bandwidth_gbps": 0,
    "create_time": "2024-11-13T12:13:52.197433"
  }
]

req_tasks = [
                {'task_name': 'iperf-server', 'qos_level': 'default'},
                {'task_name': 'iperf-client', 'qos_level': 'default'}
            ]

rel_tasks = [
    {
        'tasks_id': '702148b2-4f21-4034-af8d-d11737f9f811', 
        'domain_name': 'Nep', 
        'package_name': 'iperf-client', 
        'composite_key': 'Nep-iperf-client', 
        'version': 'v1', 
        'package_type': 'GENERIC', 
        'interoperability_tags': 'null', 
        'blacklisted_tags': 'null', 
        'task_create_time': '2024-11-13T12:13:52.190297'
    }, 
    {
        'tasks_id': '95bc75a6-b292-484d-a65d-86fae2cb6b6f', 
        'domain_name': 'Nep', 
        'package_name': 'iperf-server', 
        'composite_key': 'Nep-iperf-server', 
        'version': 'v1', 
        'package_type': 'GENERIC', 
        'interoperability_tags': 'null', 
        'blacklisted_tags': 'null', 
        'task_create_time': '2024-11-13T12:13:52.195040'
    }]

我有这个代码:

task_ids = [
    d["tasks_id"]
    for d in rel_tasks
    for t in req_tasks
    if d["package_name"] == t["task_name"]
]

resources = [
    d
    for t in req_tasks
    for d in task_resources
    if d["tasks_id"] in task_ids and d["qos_level"] == t["qos_level"]
]
print(resources)

返回:

[
    {
        "resource_id": "52a58a34-6b1b-49b7-b53d-2bc4bf72c172",
        "domain_name": "Nep",
        "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.192657",
    },
    {
        "resource_id": "c34a4411-20d3-42eb-9f74-1336ab6ab024",
        "domain_name": "Nep",
        "tasks_id": "95bc75a6-b292-484d-a65d-86fae2cb6b6f",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.197433",
    },
    {
        "resource_id": "52a58a34-6b1b-49b7-b53d-2bc4bf72c172",
        "domain_name": "Nep",
        "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.192657",
    },
    {
        "resource_id": "c34a4411-20d3-42eb-9f74-1336ab6ab024",
        "domain_name": "Nep",
        "tasks_id": "95bc75a6-b292-484d-a65d-86fae2cb6b6f",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.197433",
    },
]

这将返回列表中被重复的前两个字典。有没有办法改变我的列表理解,或者删除重复的结果,所以我得到这个:

[
    {
        "resource_id": "52a58a34-6b1b-49b7-b53d-2bc4bf72c172",
        "domain_name": "Nep",
        "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.192657",
    },
    {
        "resource_id": "c34a4411-20d3-42eb-9f74-1336ab6ab024",
        "domain_name": "Nep",
        "tasks_id": "95bc75a6-b292-484d-a65d-86fae2cb6b6f",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.197433",
    },
]
python python-3.x list list-comprehension
1个回答
0
投票

要根据内容对字典结果列表进行重复数据删除,您可以使用集合来跟踪已看到的条目和列表理解来过滤结果,这样您就可以基于

tasks_id
qos_level
进行资源重复数据删除,例如这个-

task_resources = [
    {
        "resource_id": "52a58a34-6b1b-49b7-b53d-2bc4bf72c172",
        "domain_name": "Nep",
        "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.192657"
    },
    {
        "resource_id": "8208915d-90b4-4a6c-a98c-e9eb965b4e6b",
        "domain_name": "Nep",
        "tasks_id": "702148b2-4f21-4034-af8d-d11737f9f811",
        "qos_level": "low",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.202177"
    },
    {
        "resource_id": "c34a4411-20d3-42eb-9f74-1336ab6ab024",
        "domain_name": "Nep",
        "tasks_id": "95bc75a6-b292-484d-a65d-86fae2cb6b6f",
        "qos_level": "default",
        "cpu_cores": 1,
        "memory_mb": 128,
        "storage_mb": 0,
        "cpu_flops": 19177,
        "gpu_flops": 0,
        "gpu_memory_mb": 0,
        "egress_network_bandwidth_gbps": 0,
        "ingress_network_bandwidth_gbps": 0,
        "create_time": "2024-11-13T12:13:52.197433"
    }
]

req_tasks = [
    {'task_name': 'iperf-server', 'qos_level': 'default'},
    {'task_name': 'iperf-client', 'qos_level': 'default'}
]

rel_tasks = [
    {
        'tasks_id': '702148b2-4f21-4034-af8d-d11737f9f811', 
        'domain_name': 'Nep', 
        'package_name': 'iperf-client', 
        'composite_key': 'Nep-iperf-client', 
        'version': 'v1', 
        'package_type': 'GENERIC', 
        'interoperability_tags': 'null', 
        'blacklisted_tags': 'null', 
        'task_create_time': '2024-11-13T12:13:52.190297'
    }, 
    {
        'tasks_id': '95bc75a6-b292-484d-a65d-86fae2cb6b6f', 
        'domain_name': 'Nep', 
        'package_name': 'iperf-server', 
        'composite_key': 'Nep-iperf-server', 
        'version': 'v1', 
        'package_type': 'GENERIC', 
        'interoperability_tags': 'null', 
        'blacklisted_tags': 'null', 
        'task_create_time': '2024-11-13T12:13:52.195040'
    }
]

task_ids = [
    d["tasks_id"] for d in rel_tasks for t in req_tasks if d["package_name"] == t["task_name"]
]

# Deduplicate resources based on tasks_id and qos_level
seen = set()
resources = []
for t in req_tasks:
    for d in task_resources:
        if d["tasks_id"] in task_ids and d["qos_level"] == t["qos_level"]:
            # Create a unique identifier for each resource
            identifier = (d["tasks_id"], d["qos_level"])
            if identifier not in seen:
                seen.add(identifier)
                resources.append(d)

print(resources)
© www.soinside.com 2019 - 2024. All rights reserved.