我有一份有序字典的清单。我想获取25G的端口值,其中端口6/2在100G中。
>>> from collections import OrderedDict
>>> a = [OrderedDict([('name', 601), ('100G', '6/1'), ('25G', ['6/5', '6/6', '6/7', '6/8']), ('init', '100G'), ('current', '100G')]), OrderedDict([('name', 602), ('100G', '6/2'), ('25G', ['6/9', '6/10', '6/11', '6/12']), ('init', '100G'), ('current', '100G')])]
>>> a['name']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
# something like this
if a['100G'] == '6/2':
b = a['25G']
# required output is
['6/9', '6/10', '6/11', '6/12']我试着把这个列表转换成字典,但做不到,用name我也不能访问它,有人能帮我访问所需的值吗?
发布于 2017-08-08 09:39:29
只需遍历列表:
>>> for x in a:
... if x['100G'] == '6/2':
... print x['25G']
...
['6/9', '6/10', '6/11', '6/12']或者,既然你问的是列表理解:
>>> [x['25G'] for x in a if x['100G'] == '6/2']
[['6/9', '6/10', '6/11', '6/12']]https://stackoverflow.com/questions/45557996
复制相似问题