def _groupIntersection(self, a, b):

        host_a = set(x for x in a if x[0] == 'host')
        host_b = set(x for x in b if x[0] == 'host')
        service_a = a - host_a
        service_b = b - host_b

        def intersect_service(host1, service1, service2):
            """Intersection of services.

            Both hosts and services of the other set are considered.
            For a service to make it from service2 to the return value
            it must match a host or service in host1/service1 set.
            """
            hosts = set(x[1] for x in host1)
            serviceh = set(x for x in service2 if x[1] in hosts)
            services = service1.intersection(service2)
            return serviceh.union(services)

        # Intersect the host lists
        hosts = host_a.intersection(host_b)

        # Intersect the service lists
        services = set()
        services.update(intersect_service(host_b, service_b, service_a))
        services.update(intersect_service(host_a, service_a, service_b))

        # Now recombine them
        return hosts.union(services)
Taking an intersection across hosts and services
dataset/ETHPy150Open marineam/nagcat/python/nagcat/nagios_api.py/NagiosXMLRPC._groupIntersection