繁体   English   中英

Python 3嵌套的有序字典键访问

[英]Python 3 nested ordered dictionary key access

我有一个用Python 3构建的嵌套有序字典。它看起来像这样:

coefFieldDict = OrderedDict([(('AC_Type',), 
    OrderedDict([('BADA_code', 0), ('n_engine', 1), ('eng_type', 2), 'wake_cate', 3)])), 
   (('Mass',), OrderedDic([('m_ref', 4), ('m_min', 5), ('m_max', 6), ('m_pyld', 7), ('G_w', 8), ('unused', 9)]))
   (('Flight_Env',), OrderedDict([('V_MO', 10), ('M_MO', 11), ('h_MO', 12), ('h_max', 13), ('G_t', 14), ('unused', 15)]))], ...)

现在,我想要最顶层的键列表,可以通过以下方式获得:

outerKeys = list(coefFieldDict.keys())

这给了我:

[('AC_Type',),
('Mass',),
('Flight_Env',),
('Aero',),
('Thrust',),
('Fuel',),
('Ground',)]

对于其中一个键的示例,我有:

list(coefFieldDict.keys())[1][0]

Out[104]: 'Mass'

现在,在将此有效密钥用于orderedDict('coefFieldDict')时,出现以下错误:

Traceback (most recent call last):

File "<ipython-input-107-61bab1ad7886>", line 1, in <module>

coefFieldDict['Mass']

KeyError: 'Mass'

我做错了什么?

尝试coefFieldDict[('Mass',)]

...因为您使用的是元组(为什么?)而不是字符串作为键

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM