python - 如何将多个列表值作为函数参数传递?
问题描述
有人可以帮助我理解如何将多个列表中的值作为函数参数传递吗?我正在尝试使用包含 customerId 的 url 更新每个 myemailId 的电子邮件。
到目前为止我的代码:
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
但收到与以 ... 开头的行相对应的此错误def update_email
:
TypeError: must be str, not list
提前致谢!
解决方案
首先,您不应该为每次循环迭代定义函数,而是在执行循环之前定义一次。
为了传递值,请使用:
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)
推荐阅读
- python - 将按钮添加到 Django 管理索引页面
- java - 当类型仅在运行时已知时如何指定泛型类型?
- python-3.x - 收集私人 DM 的反应但忽略机器人反应
- php - 如何加快页面加载大量图像的速度
- python - 有没有一种快速而好的方法来检测正在运行的程序的给定像素的颜色?
- haskell - 为什么会有|b|^|a| 函数a->b中的居民。它是怎么来的?为什么它不仅仅是“一个”居民
- python-3.x - Python 3 http.server - 一个奇怪的 IP 地址试图连接我的服务器
- haskell - 在编译时生成类型级别 Nats 用于约束检查
- typescript - 如何在 Angular 6+ 的 mat-paginator 中更改 itemsPerPageLabel
- reactjs - 更新父组件中的状态后,子组件不会重新渲染