本文介绍了在python中定义嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在python中定义一个嵌套字典。我尝试了以下操作:
I want to define a nested dictionary in python. I tried the following:
keyword = 'MyTest' # Later I want to pull this iterating through a list key = 'test1' sections = dict(keyword={}) #This is clearly wrong but how do I get the string representation? sections[keyword][key] = 'Some value'我可以这样做:
sections = {} sections[keyword] = {}但是Pycharm中有一个警告,说可以通过字典标签进行定义。
But then there is a warning in the Pycharm saying it can be defined through dictionary label.
有人可以指出如何实现此目标吗?
Can someone point out how to achieve this?
推荐答案keyword = 'MyTest' # Later I want to pull this iterating through a list key = 'test1' sections = {keyword: {}} sections[keyword][key] = 'Some value' print(sections) {'MyTest': {'test1': 'Some value'}}
dict(keyword = {})创建一个字符串为 keyword 的字典
dict(keyword={}) creates a dict with the string "keyword" as the key not the value of the variable keyword.
In [3]: dict(foo={}) Out[3]: {'foo': {}}在哪里使用dict实际使用变量的值如上所述。
Where using a dict literal actually uses the value of the variable as above.
更多推荐
在python中定义嵌套字典
发布评论