Setting a value in a nested python dictionary given a list of indices and value

后端 未结 8 880
忘了有多久
忘了有多久 2020-12-01 09:36

I\'m trying to programmatically set a value in a dictionary, potentially nested, given a list of indices and a value.

So for example, let\'s say my list of indices i

8条回答
  •  鱼传尺愫
    2020-12-01 10:01

    Pyhton3 provide dotty_dict lib. see documentation https://dotty-dict.readthedocs.io/en/latest/ for more clarity

    from dotty_dict import dotty
    
    dot = dotty()
    string = '.'.join(['person', 'address', 'city']) 
    dot[string] = 'New York'
    
    print(dot)
    

    output:

    {'person': {'address': {'city': 'New York'}}}
    

提交回复
热议问题