How to turn a list into nested dict in Python

前端 未结 4 1370
感情败类
感情败类 2020-12-28 21:31

Need to turn x:

X = [[\'A\', \'B\', \'C\'], [\'A\', \'B\', \'D\']]

Into Y:

Y = {\'A\': {\'B\': {\'C\',\'D\'}}}
4条回答
  •  北海茫月
    2020-12-28 22:22

    There is a logical inconsistency in your problem statement. If you really want ['xyz/123/file.txt', 'abc/456/otherfile.txt']

    to be changed to {'xyz': {'123': 'file.txt}, 'abc': {'456': 'otherfile.txt'}}

    Then you have to answer how a path 'abc.txt' with no leading folder would be inserted into this data structure. Would the top-level dictionary key be the empty string ''?

提交回复
热议问题