移除列表中的字典 Python

huangapple go评论73阅读模式
英文:

Remove dictionary in list Python

问题

以下是翻译好的部分:

我有两个字典:output 包含一个主数据集,其中包括一列相关项目,而 output_historical 包含了 output 列表中项目的一个子集。

我想将 output_historical 键的值添加到 output 的值中,但如果 output_historical 中没有值,就删除整个列表项。在下面的过度简化示例中,{'test2': {'info': '1'}} 应该从 output 中删除。

output_historical = [
    {'test1': {'result': 0}, 
     'test3': {'result': 3}
    }
]
output = [
    {'tests': 
         [
             {'test1': {'info': '0'}}, 
             {'test2': {'info': '1'}}, 
             {'test3': {'info': '2'}}
         ]
    }
]

for item in output:
    for testobject in item["tests"]:
        for test in testobject.copy():
            if test in output_historical[0]:
                print("add", test)
                testobject[test]["result"] = output_historical[0][test]["result"]
            else:
                print("del", test)
                # del ??
print(output)
# 期望输出:[{'tests': [{'test1': {'info': '0', 'result': 0}}, {'test2': {'info': '1'}}, {'test3': {'info': '2', 'result': 3}}]}]

请注意,代码部分没有进行翻译。

英文:

I have two dictionaries: output contains a main dataset including a list of relevant items and output_historical contains a subset of the items in the list of output.

I would like to add the value of the output_historical keys to the output values, but delete the whole list-item if there is no value in output_historical. In the over-simplified example below, {'test2': {'info': '1'}} should be deleted from output.

output_historical = [
    {
        'test1': {'result': 0}, 
        'test3': {'result': 3}
    }
]
output = [
    {'tests': 
         [
             {'test1': {'info': '0'}}, 
             {'test2': {'info': '1'}}, 
             {'test3': {'info': '2'}}
         ]
    }
]

for item in output:
    for testobject in item["tests"]:
        for test in testobject.copy():
            if test in output_historical[0]:
                print("add", test)
                testobject[test]["result"] = output_historical[0][test]["result"]
            else:
                print("del", test)
                # del ??
print(output)
# Expected output: [{'tests': [{'test1': {'info': '0', 'result': 0}}, {'test2': {'info': '1'}}, {'test3': {'info': '2', 'result': 3}}]}]

答案1

得分: 1

使用 remove() 方法。

由于不应从正在迭代的列表中删除元素,您应该在列表的副本上进行迭代。我认为您不需要复制 testobject,因为您在该字典中从未添加或删除任何内容。

for item in output:
    for testobject in item["tests"].copy():
        for test in testobject:
            if test in output_historical[0]:
                print("add", test)
                testobject[test]["result"] = output_historical[0][test]["result"]
            else:
                print("del", test)
                item["tests"].remove(testobject)
                break
英文:

Use the remove() method.

Since you shouldn't remove from a list that you're iterating over, you should iterate over a copy of the list. I don't think you need to make a copy of testobject, since you're never adding or removing anything in that dictionary.

for item in output:
    for testobject in item["tests"].copy():
        for test in testobject:
            if test in output_historical[0]:
                print("add", test)
                testobject[test]["result"] = output_historical[0][test]["result"]
            else:
                print("del", test)
                item["tests"].remove(testobject)
                break

</details>



huangapple
  • 本文由 发表于 2023年6月16日 05:50:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定