[英]How to create a List containing a sub dictionary keys , from nested dictionary?
Form nested dictionary, How to Create a List, containing Sub dictionary keys?表单嵌套字典,如何创建包含子字典键的列表?
dict_lbl = {
"lbl1":{"name":"label1","item1":"Accounts", "item2":"kannagu", "shortcut":"F1","printitem":"You clicked label1"},
"lbl2":{"name":"label2","item1":"Inventory", "item2":"Saragu", "shortcut":"F2","printitem":"You clicked label2"},
"lbl3":{"name":"label3","item1":"Manufacture","item2":"Thayarippu", "shortcut":"F3","printitem":"You clicked label3"},
"lbl4":{"name":"label4","item1":"PayRoll", "item2":"Sambalam", "shortcut":"F4","printitem":"You clicked label4"}
}
Need Result as follows:需要结果如下:
['name', 'item1', 'item2', 'shortcut', 'printitem']
You can use keys
:您可以使用
keys
:
output = list(dict_lbl['lbl1'].keys())
print(output) # ['name', 'item1', 'item2', 'shortcut', 'printitem']
(Actually you can omit .keys()
!) (实际上你可以省略
.keys()
!)
If you can't assume nested dictionaries have the same keys yet you want to have a result including all unique keys, you can iterate through each nested dictionary and implement set union like this:如果您不能假设嵌套字典具有相同的键,但您希望得到包含所有唯一键的结果,则可以遍历每个嵌套字典并像这样实现集合并集:
list(set().union(*[d.keys() for d in dict_lbl.values()]))
>>> ['item1', 'printitem', 'name', 'shortcut', 'item2']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.