Change values in dict of nested dicts using items in a list?

前端 未结 5 602
执念已碎
执念已碎 2020-11-29 10:09

How would you modify/create keys/values in a dict of nested dicts based on the values of a list, in which the last item of the list is a value for the dict, and the rest of

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 10:25

    Here's a recursive solution.

    def unravel(d, keys):
        i = keys[0]
        keys = keys[1:]
        tmpDict = d[i]
        if type(tmpDict) != type({}):
            return tmpDict
        else:
            return unravel(tmpDict, keys)
    

提交回复
热议问题