gpt4 book ai didi

python - 如何将新值附加到字典列表中的相同键?

转载 作者:行者123 更新时间:2023-12-03 21:59:35 25 4
gpt4 key购买 nike

我有一个测试失败列表,如下所示 -

all_failures = [
'test1/path/to/test1/log/failure_reason1',
'test1/path/to/test1/log/failure_reason2',
'test2/path/to/test2/log/failure_reason1',
'test2/path/to/test2/log/failure_reason2',
'test3/path/to/test3/log/failure_reason1',
'test4/path/to/test4/log/failure_reason1',
]

我试图通过解析列表中的每个失败来构建一个类似 JSON 的对象。到目前为止,我已经尝试编写以下代码 -
for failure in all_failures:
data = failure.split('/',1)
test = data[0]
failure_details_dict[test] = []

data = '/' + data[1]
data = data.rsplit('/', 1)

test_details_dict['path'] = data[0] + '/'
test_details_dict['reason'] = data[1]

failure_details_dict[test].append(test_details_dict)

test_details_dict = {}

for key,value in failure_details_dict.items():
print(key)
print(value)
print()

我得到的输出是 -
test4
[{'reason': 'failure_reason1', 'path': '/path/to/test4/log/'}]

test3
[{'reason': 'failure_reason1', 'path': '/path/to/test3/log/'}]

test1
[{'reason': 'failure_reason2', 'path': '/path/to/test1/log/'}]

test2
[{'reason': 'failure_reason2', 'path': '/path/to/test2/log/'}]

预期输出 是 -
{
"test1": [
{
"path": "/path/to/test1/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test1/log/",
"reason": "failure_reason2"
}

],
"test2": [
{
"path": "/path/to/test2/log/",
"reason": "failure_reason1"
},
{
"path": "/path/to/test2/log/",
"reason": "failure_reason2"
}
],
"test3": [
{
"path": "/path/to/test3/log/",
"reason": "failure_reason1"
},
],
"test4": [
{
"path": "/path/to/test4/log/",
"reason": "reason1"
},
]
}

如我们所见,我无法添加 第二个失败的路径和原因相同的 key 。示例 - test1 和 test2 有两个失败原因。

有人可以帮助了解我缺少什么吗?谢谢!

最佳答案

原因

您正在覆盖 failure_details_dict[test]对于每个循环。

解决方案

您应该只将 list 设置为一次。
您有多种选择。

  • 非pythonic方式(不推荐)

  • if test not in failure_details_dict:
    failure_details_dict[test] = []
  • 将分配替换为 dict.setdefault称呼。这种方式不会影响与 failure_details_dict 的其他交互

  • failure_details_dict.setdefault(test, [])  # instead of failure_details_dict[test] = []
  • 使用 collections.defaultdict而不是 dict .这样会影响 failure_detilas_dict 的其他互动.

  • from collections import defaultdict

    failure_details_dict = defaultdict(list) # instead of {}

    例子

    我已经重构了你的代码:

    all_failures = [
    'test1/path/to/test1/log/failure_reason1',
    'test1/path/to/test1/log/failure_reason2',
    'test2/path/to/test2/log/failure_reason1',
    'test2/path/to/test2/log/failure_reason2',
    'test3/path/to/test3/log/failure_reason1',
    'test4/path/to/test4/log/failure_reason1',
    ]

    failure_details_dict = {}

    for failure in all_failures:
    key, *paths, reason = failure.split('/')
    failure_details_dict.setdefault(key, []).append({
    'path': f"/{'/'.join(paths)}/",
    'reason': reason,
    })

    for key, value in failure_details_dict.items():
    print(key)
    print(value)
    print()

    结论
  • 如果您想要简单的更改,请使用 dict.setdefault方法。
  • 如果您多次访问 failure_details_dict并且您想要每个访问的默认值,请使用 collection.defaultdict类(class)。


  • 额外的

    How can we modify the code so that 'path' key is copied only once and only multiple dictionaries with 'reason' key is created? In general, what would be the best way to store the data in JSON format?



    您可以重新格式化您的 JSON,如:
    {
    "test1": {
    "path": "/path/to/test1/log/",
    "reason": [
    "failure_reason1",
    "failure_reason2"
    ]
    },
    "test2": {
    "path": "/path/to/test2/log/",
    "reason": [
    "failure_reason1",
    "failure_reason2"
    ]
    },
    "test3": {
    "path": "/path/to/test3/log/",
    "reason": [
    "failure_reason1"
    ]
    },
    "test4": {
    "path": "/path/to/test4/log/",
    "reason": [
    "reason1"
    ]
    }
    }

    从代码:

    all_failures = [
    'test1/path/to/test1/log/failure_reason1',
    'test1/path/to/test1/log/failure_reason2',
    'test2/path/to/test2/log/failure_reason1',
    'test2/path/to/test2/log/failure_reason2',
    'test3/path/to/test3/log/failure_reason1',
    'test4/path/to/test4/log/failure_reason1',
    ]

    failure_details_dict = {}

    for failure in all_failures:
    key, *paths, reason = failure.split('/')
    failure_details_dict.setdefault(key, {
    'path': f"/{'/'.join(paths)}/",
    'reason': [],
    })['reason'].append(reason)

    for key, value in failure_details_dict.items():
    print(key)
    print(value)
    print()

    关于python - 如何将新值附加到字典列表中的相同键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473163/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com