简体   繁体   English

通过从列表中添加元素来创建嵌套字典

[英]Creating a nested dictionary by adding elements from a list

I have the following list and dict:我有以下列表和字典:

external_list = ["122479-6-995220", "122479-2-112234", "223344-1-434312", "223344-3-575342", "223344-0-092312", "223344-4-215452", "338855-5-828822", "338855-7-234567", "338855-8-000000", "440099-9-111111"]
internal_list = ["11", "12", "13", "14", "15", "16", "17", "18", "19", "F"]

dict = {
    "122479-6-995220": "11",
    "122479-2-112234": "11",
    "223344-1-434312": "12",
    "223344-3-575342": "13",
    "223344-0-092312": "14",
    "223344-4-215452": "16",
    "338855-5-828822": "16",
    "338855-7-234567": "16",
    "338855-8-000000": "F",
    "440099-9-111111": "F"
}

I want to get this nested dict:我想得到这个嵌套的字典:

updated_dict = {
    "122479-6-995220": { "11": 1, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
    "122479-2-112234": { "11": 1, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
    "223344-1-434312": { "11": 0, "12": 1, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
    "223344-3-575342": { "11": 0, "12": 0, "13": 1, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
    "223344-0-092312": { "11": 0, "12": 0, "13": 0, "14": 1, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 0},
    "223344-4-215452": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
    "338855-5-828822": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
    "338855-7-234567": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 1, "17": 0, "18": 0, "19": 0, "F": 0},
    "338855-8-000000": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 1},
    "440099-9-111111": { "11": 0, "12": 0, "13": 0, "14": 0, "15": 0, "16": 0, "17": 0, "18": 0, "19": 0, "F": 1},
}

Basically i add to each internal list of keys all the elements from the list with values set to zero if they weren't already there, else 1.基本上,我将列表中的所有元素添加到每个内部键列表中,如果它们不存在,则值设置为零,否则为 1。

A solution that creates a brand new dictionary would also be good.创建全新词典的解决方案也很好。

Edit: Before i edited the post i used another dictionary structure, same lists:编辑:在我编辑帖子之前,我使用了另一个字典结构,相同的列表:

dict = {
    "122479-6-995220": { "11": 1},
    "122479-2-112234": { "11": 1},
    "223344-1-434312": { "12": 1},
    "223344-3-575342": { "13": 1},
    "223344-0-092312": { "14": 1},
    "223344-4-215452": { "16": 1},
    "338855-5-828822": { "16": 1},
    "338855-7-234567": { "16": 1},
    "338855-8-000000": { "F": 1},
    "440099-9-111111": { "F": 1},
}

The solution posted by metatoaster refers to this metatoaster 发布的解决方案指的是这个

external_list = ["122479-6-995220", "122479-2-112234", "223344-1-434312", "223344-3-575342", "223344-0-092312", "223344-4-215452", "338855-5-828822", "338855-7-234567", "338855-8-000000", "440099-9-111111"]
internal_list = ["11", "12", "13", "14", "15", "16", "17", "18", "19", "F"]

dict = {
    "122479-6-995220": "11",
    "122479-2-112234": "11",
    "223344-1-434312": "12",
    "223344-3-575342": "13",
    "223344-0-092312": "14",
    "223344-4-215452": "16",
    "338855-5-828822": "16",
    "338855-7-234567": "16",
    "338855-8-000000": "F",
    "440099-9-111111": "F"
}

updated_dict = {i :{j:int(j == dict[i]) for j in internal_list} for i in external_list}

You can use the above dict comprehension to get what you want.你可以使用上面的dict理解来得到你想要的。

Output: Output:

122479-6-995220 {'11': 1, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 0}
122479-2-112234 {'11': 1, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 0}
223344-1-434312 {'11': 0, '12': 1, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 0}
223344-3-575342 {'11': 0, '12': 0, '13': 1, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 0}
223344-0-092312 {'11': 0, '12': 0, '13': 0, '14': 1, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 0}
223344-4-215452 {'11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 1, '17': 0, '18': 0, '19': 0, 'F': 0}
338855-5-828822 {'11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 1, '17': 0, '18': 0, '19': 0, 'F': 0}
338855-7-234567 {'11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 1, '17': 0, '18': 0, '19': 0, 'F': 0}
338855-8-000000 {'11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 1}
440099-9-111111 {'11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, 'F': 1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM