我正在尝试收集字典中的项目:
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
我很困惑,我是python的新手。上面的结构不必是列表,我只想用key1来保存这些多个东西,就会有多个像key1这样的键。
请给我最好的建议,我也想根据顶级关键字(key1,key2等)对字典进行排序。
答案 0 :(得分:-1)
temp = {}
mydict = {}
temp["text1"] = ['a', 'b', 'c']
temp["text2"] = ['f', 'g']
temp["text3"] = ['h','i']
mydict['key1'] = list(temp.items())
mydict
输出为:
{'key1': [('text1', ['a', 'b', 'c']),
('text2', ['f', 'g']),
('text3', ['h', 'i'])]}