简体   繁体   English

Python:从评估的字符串列表中循环遍历字典参数的简便方法?

[英]Python: Easy way to loop through dictionary parameters from a list of evaluated strings?

I have a dictionary created from a json file. 我有一个从json文件创建的字典。 This dictionary has a nested structure and every few weeks additional parameters are added. 该词典具有嵌套结构,每隔几周会添加其他参数。

I use a script to generate additional copies of the existing parameters when I want multiple "legs" added. 当我想要添加多个“分支”时,我使用脚本来生成现有参数的其他副本。 So I first add the additional legs. 因此,我首先添加其他分支。 So say I start with 1 leg as my template and I want 10 legs, I will just clone that leg 9 more times and add it to the list. 假设我从1条腿作为模板开始,而我希望有10条腿,那么我将再克隆9条腿并将其添加到列表中。

Then I loop through each of the parameters (called attributes) and have to clone certain elements for each leg that was added so that it has a 1:1 match. 然后,我遍历每个参数(称为属性),并必须为添加的每条腿克隆某些元素,以使其具有1:1匹配。 I don't care about the content so cloning the first leg value is fine. 我不在乎内容,因此克隆第一回合值就可以了。

So I do the following: 因此,我执行以下操作:

    while len(data['attributes']['groupA']['params']['weights']) < legCount:
        data['attributes']['groupA']['params']['weights'].append(data['attributes']['groupA']['params']['weights'][0])

    while len(data['attributes']['groupB']['paramsGroup']['factors']) < legCount:
        data['attributes']['groupB']['paramsGroup']['factors'].append(data['attributes']['groupB']['paramsGroup']['factors'][0])

    while len(data['attributes']['groupC']['items']['delta']) < legCount:
        data['attributes']['groupC']['items']['delta'].append(data['attributes']['groupC']['items']['delta'][0])

What I'd like to do is make these attributes all strings and just loop through them dynamically so that when I need to add additional ones, I can just paste one string into my list and it works without having another while loop. 我想做的是使这些属性成为所有字符串,并动态地遍历它们,以便当我需要添加其他属性时,我可以将一个字符串粘贴到列表中,而无需另外一个while循环即可。

So I converted it to this: 所以我将其转换为:

    attribs = [
        "data['attributes']['groupA']['params']['weights']",
        "data['attributes']['groupB']['paramsGroup']['factors']",
        "data['attributes']['groupC']['items']['delta']",
        "data['attributes']['groupD']['xxxx']['yyyy']"
    ]

    for attrib in attribs:
        while len(eval(attrib)) < legCount:
            eval(attrib).append(eval(attrib)[0])

In this case eval is safe because there is no user input, just a defined list of entries. 在这种情况下,eval是安全的,因为没有用户输入,只有定义的条目列表。 Tho I wouldn't mind finding an alternative to eval either. 我也不会介意寻找eval的替代品。

It works up until the last line. 直到最后一行为止。 I don't think the .append is working on the eval() result. 我不认为.append对eval()结果起作用。 It's not throwing an error.. just not appending to the element. 它不会引发错误..只是不附加到元素。

Any ideas on the best way to handle this? 有什么最好的方法来解决这个问题吗?

Not 100% sure this will fix it, but I do notice one thing. 并非100%肯定会解决此问题,但我确实注意到一件事。

In your above code in your while condition you are accessing: 在上述条件下的上述代码中,您正在访问:

data['attributes']['groupA']['params']['weights']

then you are appending to 然后您要附加到

data['attributes']['groupA']['params']['legs']

In your below code it looks like you are appending to 'weights' on the first iteration. 在下面的代码中,您似乎在第一次迭代中附加了'weights' However, this doesn't explain the other attributes you are evaluating... just one red flag I noticed. 但是,这并不能解释您正在评估的其他属性……我注意到一个危险信号。

Actually my code was working. 实际上我的代码在工作。 I was just checking the wrong variable. 我只是在检查错误的变量。 Thanks Me! 谢谢我! :) :)

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

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