def _node_merge_dict(primary_label, primary_key, nodes):
    """ Convert a set of :class:`.Node` objects into a dictionary of
    :class:`.Node` lists, keyed by a 3-tuple of
    (primary_label, primary_key, frozenset(labels)).

    :param primary_label:
    :param primary_key:
    :param nodes:
    :return: dict of (p_label, p_key, frozenset(labels)) to list(nodes)
    """
    d = {}
    for node in nodes:
        p_label = getattr(node, '__primarylabel__', None) or primary_label
        p_key = getattr(node, '__primarykey__', None) or primary_key
        key = p_label, p_key, frozenset(node.labels)
        d.setdefault(key, []).append(node)
    return d
