[英]I need help using Dictionary of dictionary python
I am trying to collect items in a dictionary:我正在尝试收集字典中的项目:
enter code here
key1:
[
('text1', ['a', 'b', 'c']),
('text2', ['f', 'g']),
('text3',['h','i'])
]
#I tried to use collections.defaultdict() as
mydict = collections.defaultdict(list)
temp = collections.defaultdict(list)
temp["text1"].append(['a', 'b', 'c'])
temp["text2"].append(['f', 'g'])
temp["text3"].append(['h','i'])
mydict[key] = temp
I am confused and I am new to python.我很困惑,我是 python 的新手。 Above structure doesn't need to be a list I just wanted to hold these multiple things with key1, there would be multiple keys like key1.
上面的结构不需要是一个列表,我只是想用 key1 保存这些多个东西,会有多个键,比如 key1。
Please advice me the best way I can do it, Also I want to sort this dictionary based on the top level keys (key1, key2 etc).请以最好的方式给我建议,我也想根据顶级键(key1、key2 等)对这本字典进行排序。
temp = {}
mydict = {}
temp["text1"] = ['a', 'b', 'c']
temp["text2"] = ['f', 'g']
temp["text3"] = ['h','i']
mydict['key1'] = list(temp.items())
mydict
the output is:输出是:
{'key1': [('text1', ['a', 'b', 'c']),
('text2', ['f', 'g']),
('text3', ['h', 'i'])]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.