简体   繁体   English

如何将列表值插入 Python 中的字典?

[英]How to insert list values to a dictionary in Python?

How can I insert multiple emailAddress fields to attendees list in dictionary?如何在字典中的与会者列表中插入多个 emailAddress 字段? I have two list addresses, names.我有两个列表地址,名字。 And I want to loop through these lists to generate this dict.我想遍历这些列表来生成这个字典。 FE FE

addresses = ["test@test.test", "nana@nana.nana"]
names = ["test", "nana"] 

Result should be like this:结果应该是这样的:

{
    "subject": "Let's go for lunch",
    "body": {
        "contentType": "HTML",
        "content": "Does mid month work for you?"
    },
    "start": {
        "dateTime": "2020-02-10T12:00:00",
        "timeZone": "Pacific Standard Time"
    },
    "end": {
        "dateTime": "2020-02-10T14:00:00",
        "timeZone": "Pacific Standard Time"
    },
    "location": {
        "displayName": "Harry's Bar"
    },
    "attendees": [
        {
            "emailAddress": {
                "address": "test@test.test",
                "name": "test"
            },
            "type": "required"
        },
        {
            "emailAddress": {
                "address": "nana@nana.nana",
                "name": "nana"
            },
            "type": "required"
        }
    ],
    "isOnlineMeeting": true,
    "onlineMeetingProvider": "teamsForBusiness"
}

I have tried to loop through each list and create a string:我试图遍历每个列表并创建一个字符串:

for a in addresses:
    for n in names:
        attendees_body +='{"emailAddress": {"address": "%s", "name": "%s"}, "type": "required"},' % (a,n)

Then I tried to add this string to a dictionary, and to use json.dumps(dictionary) to format json file (which I need).然后我尝试将此字符串添加到字典中,并使用 json.dumps(dictionary) 格式化 json 文件(我需要)。 But then I got result which looks like this但后来我得到了看起来像这样的结果

{"subject": "subjectas", "start": {"dateTime": "2020-02-10T12:00:00", "timeZone": "Pacific Standard Time"}, "end": {"dateTime": "2020-02-10T14:00:00", "timeZone": "Pacific Standard Time"}, "location": {"displayName": "Harry's Bar"}, "attendees": ["{'emailAddress': {'address': 'sasi@sasi.xui', 'name': 'zdarovenko'}, 'type': 'required'},{'emailAddress': {'address': 'sasi@sasi.xui', 'name': 'kurwenko'}, 'type': 'required'},{'emailAddress': {'address': 'nana@sasi.xui', 'name': 'zdarovenko'}, 'type': 'required'},{'emailAddress': {'address': 'nana@sasi.xui', 'name': 'kurwenko'}, 'type': 'required'},"], "isOnlineMeeting": true, "onlineMeetingProvider": "teamsForBusiness"}

Where some values are in "" and some in ''..有些值在 "" 中,有些在 ''..

It's not super clear what you are asking but in essence I think it's a question of two parts:目前还不清楚你在问什么,但本质上我认为这是一个两部分的问题:

  1. Iterate through two different lists together一起遍历两个不同的列表
  2. Insert the items from these lists into an existing Python dictionary with an existing list将这些列表中的项目插入具有现有列表的现有 Python 字典

Assuming that's correct then you can simply use the append method on the dictionary's list.假设这是正确的,那么您可以简单地使用字典列表中的 append 方法。 The function you need to use to get the aggregated data from the two lists is resolved using zip which takes iterables (a list in this case) then aggregates them in a tuple.您需要用于从两个列表中获取聚合数据的 function 使用zip解析,该 zip 采用可迭代对象(在本例中为列表),然后将它们聚合到一个元组中。

I should say that json is not required here, I just used it instead of pprint to get the formatting nice.我应该说这里不需要 json,我只是用它而不是 pprint 来获得很好的格式。

    import json
    from collections import defaultdict
    
    addresses = ["test@test.test", "nana@nana.nana"]
    names = ["test", "nana"]
    
    my_dictionary = {
        "subject": "Let's go for lunch",
        "body": {
            "contentType": "HTML",
            "content": "Does mid month work for you?"
        },
        "start": {
            "dateTime": "2020-02-10T12:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "end": {
            "dateTime": "2020-02-10T14:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "location": {
            "displayName": "Harry's Bar"
        },
        "attendees": [
    
        ],
        "isOnlineMeeting": True,
        "onlineMeetingProvider": "teamsForBusiness"
    }
    
    for a, n in zip(addresses, names):
        contact = {
                    "emailAddress":
                        {
                            "address": a,
                            "name": n
                        },
                    "type": "required"
                  }
        my_dictionary['attendees'].append(contact)
    
    print(json.dumps(my_dictionary, indent=4, default=str))

This would yield the following result:这将产生以下结果:

 {
        "subject": "Let's go for lunch",
        "body": {
            "contentType": "HTML",
            "content": "Does mid month work for you?"
        },
        "start": {
            "dateTime": "2020-02-10T12:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "end": {
            "dateTime": "2020-02-10T14:00:00",
            "timeZone": "Pacific Standard Time"
        },
        "location": {
            "displayName": "Harry's Bar"
        },
        "attendees": [
            {
                "emailAddress": {
                    "address": "test@test.test",
                    "name": "test"
                },
                "type": "required"
            },
            {
                "emailAddress": {
                    "address": "nana@nana.nana",
                    "name": "nana"
                },
                "type": "required"
            }
        ],
        "isOnlineMeeting": true,
        "onlineMeetingProvider": "teamsForBusiness"
    }

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

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