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',)]
...因为您使用元组(为什么?)而不是字符串作为键
我有一个嵌套的、有序的内置字典 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',)]
...因为您使用元组(为什么?)而不是字符串作为键