简体   繁体   English

从for循环中的列表追加参数

[英]Appending parameters from a list in a for loop

I have a list with several data, ['a','b','c'...] 我有一个包含几个数据的列表,['a','b','c'...]

The goal here is to read all the items in the list and access to a json file to retrieve that information. 这里的目标是读取列表中的所有项目并访问json文件以检索该信息。

The json i have is as follows 我拥有的json如下

{
"a": {  
"b": { 
"c": { .... }
}
} }

So, the final sentence to execute is 因此,要执行的最后一句话是

code.get(list[0]).get(list[1]).get(list[2]...get(list[n]

Is there any way i can do a for loop based on the length of the list to do this? 有什么办法可以基于列表的长度执行for循环吗?

Something like, for any item in the list, append a ..get(list[i]) to my sentence 对于列表中的任何项目,在我的句子后面附加一个..get(list [i])

Thanks 谢谢

Just iterate over the key list and go down into the code dict: 只需遍历键列表,然后进入code字典即可:

keys =  ['a','b','c']
current_level = code # Top level
for key in keys:
    current_level = current_level.get(key) # Descent next level
print current_level # Value of the 'c' dict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM