简体   繁体   English

当一本字典是Python中的列表时,如何将它插入另一本字典?

[英]How to insert one dictionary into another dictionary when it is a list in Python?

I have one dictionary under a list and i want to insert another dictionary into that list.我在列表下有一本字典,我想在该列表中插入另一本字典。 So need help in Python.所以需要 Python 方面的帮助。

custom_fields = {
    "183": issue['Sprint#'],
    "7": issue['AffectedVersion'],
    "11": issue['Severity'],
    "13": issue['Tag'],
    "180": issue['Tester root cause'],
    "93": issue['Testcase ID'],
    "150": issue['Subsystem']
}
custom_field_payload = {'custom_value_fields':custom_fields.items()}
payload = {'issue' : dict(payload.items()+default_payload.items())}

Inside the issue, i want custom_field_payload.在问题中,我想要 custom_field_payload。 Please help me to resolve.请帮我解决。

Expected output:预期输出:

{"issue": {
    "custom_field_values": {
        "183" : "Sprint-1",
        "7" : "12.5.0",
        "11" : "Low (P4)",
        "13" : "Functional",
        "180" : "Legacy Issue identified in Sprint",
        "93" : "CERT-20083926:FN_Update_Gemalto_HSMSettings_Available_Status_DefaultOptionEnabled_ChangeInvalidPassword",
        "150" : "CERTIFICATE"
    },
    "project_id" : 25,
    "category_id": 2380
}}

Try the below, simply add list to items stuff:尝试以下操作,只需将列表添加到items内容:

custom_field_payload = {'custom_value_fields':custom_fields.items()}
payload = {'issue' : dict(list(payload.items())+list(custom_field_payload.items()))}

Try this:尝试这个:

issue = {'Sprint#': "Sprint-1",
         'AffectedVersion': "12.5.0",
         'Severity': "Low (P4)",
         'Tag': "Functional",
         'Tester root cause': "Legacy Issue identified in Sprint",
         'Testcase ID': "CERT-20083926:FN_Update_Gemalto_HSMSettings_Available_Status_DefaultOptionEnabled_ChangeInvalidPassword",
         'Subsystem': "CERTIFICATE"}

default_payload = {"project_id": 25, "category_id": 2380}
custom_fields = {
    "183": issue['Sprint#'],
    "7": issue['AffectedVersion'],
    "11": issue['Severity'],
    "13": issue['Tag'],
    "180": issue['Tester root cause'],
    "93": issue['Testcase ID'],
    "150": issue['Subsystem']
}
custom_field_payload = {'custom_field_values':custom_fields}
p = {'issue' : custom_field_payload}
payload = default_payload.copy()
payload.update(p)

print(payload)

I'm guessing the error you're experiencing goes as follows: TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' .我猜您遇到的错误如下: TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items' Let's interpret this error.让我们解释一下这个错误。

TypeError -> first-off, we can see that this is a TypeError , that means that we've made an error with which type(s) we're using. TypeError -> 首先,我们可以看到这是一个TypeError ,这意味着我们对使用的类型犯了错误。

unsupported operand type(s) for +: -> this is telling us that we've tried to add two operands, but their type was unsupported (invalid). unsupported operand type(s) for +: -> 这告诉我们我们已经尝试添加两个操作数,但它们的类型不受支持(无效)。

'dict_items' and 'dict_items' -> finally, this tells us that we've tried to add two dict_items , which is the method items inside of dict class ( dict.items ). 'dict_items' and 'dict_items' -> 最后,这告诉我们我们已经尝试添加两个dict_items ,这是dict类( dict.items )内的方法items

Now we know that the error was gotten because we tried to add two dict.items which isn't supported, so we can't proceed to add those two.现在我们知道错误是因为我们试图添加两个dict.items ,所以我们不能继续添加这两个。

What I think you tried to do is insert default_payload to the payload dictionary.我认为您尝试做的是将default_payload插入到payload字典中。 In order to do that, you can replace your payload dictionary with this:为了做到这一点,你可以用这个替换你的payload字典:

payload = {'issue': custom_field}
payload.update(default_payload)

The dict.update method inserts a dictionary into another dictionary. dict.update方法将字典插入到另一个字典中。

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

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