Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

python - 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.

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

question from:https://stackoverflow.com/questions/65844470/creating-a-nested-dictionary-by-adding-elements-from-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.

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}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...