[英]How to iterate through list and pass each element as list containing itself as the only element?
I am interacting with an API that requires a list containing only one element be passed as a parameter. 我正在与一个API交互,该API要求仅包含一个元素的列表作为参数传递。
I am trying to perform the same operation for each element in a list of elements. 我正在尝试对元素列表中的每个元素执行相同的操作。
This is my simple example code: 这是我的简单示例代码:
category_ids = 13
temp = svc.call(session, 'catalog_category.assignedProducts', [category_ids])
This works well. 这很好。
What I would like to do is to define category_ids
to have several elements, such as category_ids = [12, 13, 14, 15]
and perform the call to API where [category_ids]
references each element in the list. 我想做的是将category_ids
定义为具有几个元素,例如category_ids = [12, 13, 14, 15]
并执行对API的调用,其中[category_ids]
引用列表中的每个元素。
Is this possible? 这可能吗?
You could wrap this in a function (if I understand you correctly): 您可以将其包装在一个函数中(如果我理解正确的话):
def my_call(category_ids):
return [svc.call(session, 'catalog_category.assignedProducts', [category_id]) for category_id in category_ids]
temp = []
for category_id in category_ids:
temp.append(svc.call(session, 'catalog_category.assignedProducts', [category_id])
After that you will have results of individual calls (for individual category_id
s) in the temp
list. 之后,您将在temp
列表中获得单个调用的结果(针对单个category_id
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.