<jupyter_start><jupyter_text>Extract all Families that have held a Kingdom or Empire Level Title Below is a list of all Dynasties who have held Kingdom or Empire level titles and a list of the titles that they held. These titles may not have been held by a single person at the same time or were maybe held by two seperate branches at the same time.<jupyter_code>pipeline = [
    {
        "$match" : {"$or" : [{"title" : {"$regex" : '^k_.*'}}, {"title" : {"$regex" : '^e_.*'}}]}
    },
    {
        "$unwind" : "$holders"
    },
    {
        "$lookup" :
        {
            "from" : "characters",
            "localField" : "holders.holder",
            "foreignField" : "_id",
            "as" : "character"
        }
    },
    {
        "$unwind" : "$character"
    },
    {
        "$lookup" :
        {
            "from" : "dynasties",
            "localField" : "character.dnt",
            "foreignField" : "_id",
            "as" : "dynasty"
        }
    },
    {
        "$unwind" : "$dynasty"
    },
    {
        "$group" : { "_id" : "$dynasty._id", "name" : {"$first" : "$dynasty.name"}, "title" : { "$addToSet": "$title" } } 
    },
    {
        "$sort" : {"name" : 1}
    }
    
   ]

for title in titles.aggregate(pipeline):
    print(title)<jupyter_output>{'_id': 101727, 'name': 'Abbasid', 'title': ['e_arabia', 'k_armenia']}
{'_id': 1044115, 'name': 'Abbonid', 'title': ['k_england', 'k_france']}
{'_id': 1022207, 'name': 'Aemilius', 'title': ['e_byzantium']}
{'_id': 1022211, 'name': 'Africanus', 'title': ['e_byzantium']}
{'_id': 1044033, 'name': 'Agilolfing', 'title': ['k_bavaria']}
{'_id': 1044053, 'name': 'Alachisling', 'title': ['k_france', 'k_italy', 'k_croatia', 'k_lotharingia']}
{'_id': 10322359, 'name': 'Alishi', 'title': ['k_khiva']}
{'_id': 9617, 'name': 'Amaling', 'title': ['k_italy']}
{'_id': 101737, 'name': 'Anchabadze', 'title': ['k_georgia', 'e_byzantium']}
{'_id': 1044092, 'name': 'Anniona', 'title': ['k_bavaria']}
{'_id': 1044051, 'name': 'Ansprandling', 'title': ['k_italy']}
{'_id': 1022216, 'name': 'Antoninus', 'title': ['e_byzantium']}
{'_id': 1022179, 'name': 'Apsimaros', 'title': ['e_byzantium']}
{'_id': 1046005, 'name': 'Arabid', 'title': ['k_castille', 'e_spain']}
{'_id': 1022184, 'name': 'Arabissinos', 'title': ['[...]
{'fraction_non_alphanumeric': 0.22975929978118162, 'fraction_numerical': 0.04463894967177243, 'mean_word_length': 3.2099447513812156, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '306886', 'license': 'MIT', 'n_tokens_mistral': 965, 'n_tokens_neox': 883, 'n_words': 207, 'path': 'notebooks/7.1 Titles per Dynasty.ipynb', 'repo_name': 'maniacalbrain/ck2-social-networks', 'size': '2285'}