Python 访问嵌套在两个键中的值
假设我有下面的格言:Python 访问嵌套在两个键中的值,python,python-2.7,logic,Python,Python 2.7,Logic,假设我有下面的格言: L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}} 如何访问Python 2.7中的键root[1]、root[2]、root[3]、root[4](root[]的索引是动态的)的值请尝试: >>> L = {'A': {'root[1]': 'firstvalue'
L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}}
如何访问Python 2.7中的键root[1]、root[2]、root[3]、root[4]
(root[]的索引是动态的)的值请尝试:
>>> L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}}
>>> L['A']['root[1]']
'firstvalue'
>>> L['A']['root[2]']
'secondvalue'
>>> L['B']['root[3]']
'thirdvalue'
>>> L['B']['root[4]']
'Fourthvalue'
>>>
为了从嵌套在dict中的dict访问值,我使用了以下步骤:
L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}}
Solution:
F = {}
G = []
F = L.get("A", None)
F= {{'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}}
for value in F.values():
G.append(value)
Output:
G = ['firstvalue', 'secondvalue']
大概是这样的:
for (key, value) in L.items():
for (another_key, real_value) in value.items():
print(another_key, real_value)
L['A']['root[1]]
,L['B']['root[3]]]
,等等。如果你问的不是那么简单,你可能需要对这个问题进行更多的解释。