转换值列表以用作字典键

2024-09-24 06:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试转换用于在字典中查找特定键值的值列表。你知道吗

我想不出一个可行的方法。你知道吗

尝试将列表转换为字符串并将其作为键传递给字典,但由于列表中也包含整数值,因此现在可以使用。你知道吗

l = ['tsGroups', 0, 'testCases', 0, 'parameters', 'GnbControlAddr', 'ip']

d={
"tsGroups": [{"tsId": 19,
"testCases": [{"name": "abcd",
               "type": "xyz",
               "parameters": {"GnbControlAddr": 
                             {"ip": "192.1.1.1",
                              "mac": "",
                              "mtu": 1500,
                              "phy": "eth2",
                              }
                              }
             }]
             }]
  }

print(d["tsGroups"][0]["testCases"][0]["parameters"]["GnbControlAddr"] 
["ip"])

需要将输入列表“l”转换为用作

d["tsGroups"][0]["testCases"][0]["parameters"]["GnbControlAddr"]["ip"]

Tags: 方法字符串nameip列表字典type整数
2条回答

无法说明这有多像python,但循环遍历列表并更新对新数据结构的引用似乎是可行的:

current = d
for key in l:
    current = current[key]
print current
In [5]: d={ 
   ...: "tsGroups": [{"tsId": 19,"testCases": [{"name": "abcd","type": "xyz", 
   ...:            "parameters": {"GnbControlAddr": { 
   ...:                  "ip": "192.1.1.1", 
   ...:                  "mac": "", 
   ...:                  "mtu": 1500, 
   ...:                  "phy": "eth2", 
   ...: } 
   ...:     }}]}]}                                                                                                                                                                                                                                                                                                            

In [6]: L = ['tsGroups', 0, 'testCases', 0, 'parameters', 'GnbControlAddr', 'ip']                                                                                                                                                                                                                                             

In [7]: functools.reduce?                                                                                                                                                                                                                                                                                                     
Docstring:
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
Type:      builtin_function_or_method

In [8]: t = d                                                                                                                                                                                                                                                                                                                 

In [9]: for e in L: t = t[e]                                                                                                                                                                                                                                                                                                  

In [10]: t                                                                                                                                                                                                                                                                                                                    
Out[10]: '192.1.1.1'

相关问题 更多 >