Python Generate a dynamic dictionary from the list of keys

前端 未结 7 1533
长发绾君心
长发绾君心 2021-02-09 02:38

I do have a list as given below -

keyList1 = [\"Person\", \"Male\", \"Boy\", \"Student\", \"id_123\", \"Name\"]
value1 = \"Roger\"

How can I ge

7条回答
  •  野的像风
    2021-02-09 03:03

    Create your own class derived from dict where the init method takes a list and a single value as inputs and iterate through the list setting the keys to value, define an update method that takes a list and a new value and for each item that is not already a key set it to the new value, (assuming that is what you need).

    Forget the idea of

    mydict["Person"]["Male"]["Boy"]["Student"]["id_123"]["Name"] = value1`

    as it is confusing with subindexes.

提交回复
热议问题