访问值假设我有以下字典:其嵌套在2个键
L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}}
如何访问密钥root[1], root[2], root[3], root[4]
的值(根的索引[]是动态的)在Python 2.7
。
访问值假设我有以下字典:其嵌套在2个键
L = {'A': {'root[1]': 'firstvalue', 'root[2]': 'secondvalue'}, 'B': {'root[3]': 'thirdvalue', 'root[4]': 'Fourthvalue'}}
如何访问密钥root[1], root[2], root[3], root[4]
的值(根的索引[]是动态的)在Python 2.7
。
尝试:
>>> 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'
>>>
从嵌套在字典里的字典访问值,我使用的以下步骤:
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)
'大号['A'] ['root [1]']','L ['B'] ['root [3]']'等等。如果你问的不是那么简单,你可能需要添加对问题的更多解释。 – glibdud