【发布时间】:2021-03-02 10:21:50
【问题描述】:
我有以下字典
dictionary = {'test1.txt': {'apple': 1, 'banana': 1, 'lemon': 1},
'test2.txt': {'apple': 1, 'banana': 1},
'test3.txt': {'apple': 1, 'lemon': 2},
'test4.txt': {'apple': 1, 'lemon': 1, 'grape': 1}}
我希望输出是
[['', 'apple', 'banana', 'lemon', 'grape'],
['test1.txt',1,1,1,0],
['test2.txt',1,1,0,0],
['test3.txt',1,0,2,0],
['test4.txt',1,0,1,1]]
我正在尝试的方式是这样的
testcasenumber = dictionary.keys() // here i got all the test.txts
我是 python 新手,有点卡在如何继续我的解决方案。
【问题讨论】:
-
一个简单的谷歌搜索会产生很多结果。例如,geeksforgeeks.org/python-flatten-nested-dictionary-to-matrix
-
感谢您的建议,我检查了但发现有点不同
-
我同意你的观点,我可以添加完整的信息,对不起,如果我再次发布问题,我一定会保留你的所有建议并以更有效的方式写作.
标签: python list multidimensional-array nested-lists