简体   繁体   English

如何将多个列表值作为 function 参数传递?

[英]How to pass multiple list values as function parameters?

Would someone be able to help me understanding how can I pass values from multiple lists as function parameters?有人可以帮助我理解如何将多个列表中的值作为 function 参数传递吗? I'm trying to update email for each of myemailId with url that includes customerId.我正在尝试使用包含 customerId 的 url 为每个 myemailId 更新 email。

my code so far:到目前为止我的代码:

emailId = [732853380,7331635674]
customerId = ['cust-12345-mer','cust-6789-mer']

for x, y in zip(emailId, customerId):
    def update_email(emailId, token, user, notes="https://myurl.com/customer?customerId =" + customerId):
        headers = {     'accept': 'application/json',
                    'Content-Type': 'application/json',
                    'token': token,
                    'user': user}
        endpoint = 'email/'
        body = {'emailId': emailId, 'user': user, 'notes': notes}
        requests.put(url = host + endpoint, headers = headers, json=body)
        return True

but receiving this error that is corresponding to the line that starts with def update_email ...:但收到与以def update_email ... 开头的行对应的此错误:

TypeError: must be str, not list

Thanks in advance!提前致谢!

First of all, you shouldn't define the function for each loop iteration but once before executing the loop.首先,您不应该为每次循环迭代定义 function,而应在执行循环之前定义一次。

In order to pass the values, use:为了传递值,请使用:

emailId = [732853380, 7331635674]
customerId = ['cust-12345-mer', 'cust-6789-mer']


def update_email(emailId, token, user, customerId):
    notes = "https://myurl.com/customer?customerId =" + customerId
    headers = {'accept': 'application/json',
               'Content-Type': 'application/json',
               'token': token,
               'user': user}
    endpoint = 'email/'
    body = {'emailId': emailId, 'user': user, 'notes': notes}
    requests.put(url=host + endpoint, headers=headers, json=body)
    return True


for x, y in zip(emailId, customerId):
    update_email(x, token, user, y)

customerId is the list, y is the value from that list. customerId是列表, y是该列表中的值。 Use利用

notes="https://myurl.com/customer?customerId =" + y

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

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