简体   繁体   English

如何使用循环更改字典中数组值的特定键值?

[英]How to change specific key values with array values in dictionary using loop?

I have 1 dictionary and 1 array.我有 1 个字典和 1 个数组。 My task is to run through dictionary and create 5 separete dictionaries where key "email" will be replaced with array values.我的任务是遍历字典并创建 5 个单独的字典,其中键“email”将替换为数组值。 All my attempts to create loops just use the last array's value so there is only one dict.我创建循环的所有尝试都只使用最后一个数组的值,所以只有一个字典。 How to loop it correctly to solve it如何正确循环才能解决

data = {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": True,
        "first_name": "TestUser",
        "last_name": "One"

} }

emails_list = ['mailtest.unconfirm@yahoo.com',
        'mailtest.unconfirm@gmail.com',
        'mailtest.unconfirm@live.com',
        'mailtest.unconfirm@outlook.com',
        'mailtest.unconfirm@icloud.com'
        ]

You can do this in a single line with python!您可以使用 python 在一行中完成此操作! See below见下文

result = [{**data,**{"email": val}} for val in emails_list]

This creates a list of N dicts, as per length of your emails.这将根据您的电子邮件的长度创建一个包含 N 个字典的列表。 resulting in导致

[
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@gmail.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@live.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@outlook.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    },
    {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@icloud.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": true,
        "first_name": "TestUser",
        "last_name": "One"
    }
]

I might be misunderstanding what you want, but can't you just loop through the array and individually set the dictionary's email and copy that?我可能误解了你想要什么,但你不能只循环遍历数组并单独设置字典的 email 并复制它吗? Example below:下面的例子:

emails_dict_ls = []
for email in emails_list:
     data_copy = data.copy()
     data_copy["email"] = email
     emails_dict_ls.append(data_copy)
print(emails_dict_ls)

This will print out the following这将打印出以下内容

[{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@yahoo.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@gmail.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@live.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@outlook.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'},
{'company': 'Company', 'phone': '+1111111', 'email': 'mailtest.unconfirm@icloud.com', 'password1': 'defaultpassword', 'password2': 'defaultpassword', 'terms_agree': True, 'first_name': 'TestUser', 'last_name': 'One'}]

You need to make a deep copy:您需要进行深拷贝:

import copy

def new_dic(email,data_dic):
    new_dic = copy.deepcopy(data_dic); new_dic["email"] = email
    return(new_dic)

data = {
        "company": "Company",
        "phone": "+1111111",
        "email": "mailtest.unconfirm@yahoo.com",
        "password1": "defaultpassword",
        "password2": "defaultpassword",
        "terms_agree": True,
        "first_name": "TestUser",
        "last_name": "One"}

emails_list = ['mailtest.unconfirm@yahoo.com',
        'mailtest.unconfirm@gmail.com',
        'mailtest.unconfirm@live.com',
        'mailtest.unconfirm@outlook.com',
        'mailtest.unconfirm@icloud.com'
        ]

dic_list = []
for x in range(0,len(emails_list)):
    dic_list.append(new_dic(emails_list[x],data))

print(dic_list[0]['email'])
'mailtest.unconfirm@yahoo.com'
print(dic_list[-1]['email'])
'mailtest.unconfirm@icloud.com'

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

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