Python Generate a dynamic dictionary from the list of keys

前端 未结 7 1374
长发绾君心
长发绾君心 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:12

    I am trying to dealing with similar stuff, so I can suggest some guidelines, but again I am naive in Python, so this is just a guideline...

    you have a list of keys , so you can definitely start with a loop iterating for each value and then assign the value

    like

    for i in keylist:
    if type(keylist[i]) == dict:
            do something
        else:
            keylist[i] = {}
    

    in the do something, you need to increment i and change index to [i][i+1] and then follow the same untill i+n = len(keylist)

    0 讨论(0)
提交回复
热议问题