简体   繁体   中英

Nested DICT in Python3 from 2 other DICTs

I have these two dict objects:

d1={A:B,...}
d2={A:(C,D),...}

I want to create a third dict from them both:

d3={D:{C:{A:B}...}...}

This is what I do:

d3={}
for A in list(d1.keys()):
  d3.setdefault(A,{}).update(d3[A].setdefault(d1[A],{}).update({d2[A][0]:d2[A][1]}))

and it works perfectly (as per the resulted data in d3), except for this error:

TypeError: 'NoneType' object is not iterable

Should I safely disregard the error? Can I possibly make it without errors?

EDITED to provide sample data:

d1={9024: 92, 3905: 10, 1922: 74, 2564: 14, 3527: 6, 5578: 9, 1863: 36, 3660: 35, 216: 27, 8334: 16, 8272: 72, 7249: 93, 9426: 45, 659: 35, 1327: 84, 6357: 1, 2519: 7, 6872: 40, 6489: 86, 3503: 59, 5710: 90, 4319: 98, 6112: 43, 6947: 61, 741: 2, 9510: 86, 8807: 19, 4457: 38, 2086: 83, 807: 46, 8559: 97, 9457: 55, 937: 98, 4411: 10, 7483: 4, 3018: 29, 8277: 45}
d2={9024: ('127', '1'), 3905: ('125', '1'), 1922: ('124', '1'), 2564: ('125', '1'), 7483: ('126', '1'), 3527: ('125', '1'), 5710: ('126', '1'), 5578: ('126', '1'), 3660: ('125', '1'), 8334: ('127', '1'), 8272: ('127', '1'), 7249: ('126', '1'), 9426: ('127', '1'), 659: ('124', '1'), 6357: ('126', '1'), 2519: ('125', '1'), 6872: ('126', '1'), 6489: ('126', '1'), 3503: ('125', '1'), 216: ('124', '1'), 4319: ('125', '1'), 6112: ('126', '1'), 9457: ('127', '1'), 6947: ('126', '1'), 741: ('124', '1'), 9510: ('127', '1'), 8807: ('127', '1'), 8559: ('127', '1'), 4457: ('125', '1'), 1863: ('124', '1'), 1327: ('124', '1'), 807: ('124', '1'), 937: ('124', '1'), 4411: ('125', '1'), 2086: ('124', '1'), 3018: ('125', '1'), 8277: ('127', '1')}

d3={'1': {'127': {9024: 92, 8272: 72, 9426: 45, 8277: 45, 9510: 86, 8807: 19, 9457: 55, 8334: 16, 8559: 97}, '126': {6112: 43, 7249: 93, 6947: 61, 6357: 1, 6872: 40, 6489: 86, 5578: 9, 7483: 4, 5710: 90}, '125': {3905: 10, 4411: 10, 2564: 14, 2519: 7, 4457: 38, 3018: 29, 3527: 6, 3660: 35, 4319: 98, 3503: 59}, '124': {1922: 74, 659: 35, 741: 2, 2086: 83, 1863: 36, 216: 27, 937: 98, 807: 46, 1327: 84}}}

The logic of your code is a little hard to follow but here's a simplified form that gives the expected output:

>>> d1={9024: 92, 3905: 10, 1922: 74, 2564: 14, 3527: 6, 5578: 9, 1863: 36, 3660: 35, 216: 27, 8334: 16, 8272: 72, 7249: 93, 9426: 45, 659: 35, 1327: 84, 6357: 1, 2519: 7, 6872: 40, 6489: 86, 3503: 59, 5710: 90, 4319: 98, 6112: 43, 6947: 61, 741: 2, 9510: 86, 8807: 19, 4457: 38, 2086: 83, 807: 46, 8559: 97, 9457: 55, 937: 98, 4411: 10, 7483: 4, 3018: 29, 8277: 45}
>>> d2={9024: ('127', '1'), 3905: ('125', '1'), 1922: ('124', '1'), 2564: ('125', '1'), 7483: ('126', '1'), 3527: ('125', '1'), 5710: ('126', '1'), 5578: ('126', '1'), 3660: ('125', '1'), 8334: ('127', '1'), 8272: ('127', '1'), 7249: ('126', '1'), 9426: ('127', '1'), 659: ('124', '1'), 6357: ('126', '1'), 2519: ('125', '1'), 6872: ('126', '1'), 6489: ('126', '1'), 3503: ('125', '1'), 216: ('124', '1'), 4319: ('125', '1'), 6112: ('126', '1'), 9457: ('127', '1'), 6947: ('126', '1'), 741: ('124', '1'), 9510: ('127', '1'), 8807: ('127', '1'), 8559: ('127', '1'), 4457: ('125', '1'), 1863: ('124', '1'), 1327: ('124', '1'), 807: ('124', '1'), 937: ('124', '1'), 4411: ('125', '1'), 2086: ('124', '1'), 3018: ('125', '1'), 8277: ('127', '1')}
>>> d3 = {}
>>> for A in d1:
...    d3.setdefault(d2[A][1], {}).setdefault(d2[A][0], {}).update({A:d1[A]})
>>> d3
{'1': {'124': {216: 27,
   659: 35,
   741: 2,
   807: 46,
   937: 98,
   1327: 84,
   1863: 36,
   1922: 74,
   2086: 83},
   ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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