简体   繁体   English

如何遍历list并将每个元素作为包含自身作为唯一元素的list传递?

[英]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.

相关问题 遍历列表中的元素 - Iterate Through Element in List 如何遍历 2 个巨大的列表并查找列表 1 中的每个元素是否是列表 2 元素的一部分? - How to iterate through 2 huge lists and find if each element in list 1 is a part of an element of list 2? 如何将 append 列表的每个元素复制到自身? - How to append a copy of each element of a list to itself? 如何迭代和打印列表中的每个元素(Python) - How to iterate and print each element in list (Python) 如何逐个元素地遍历嵌套列表元素以每次创建不同的字符串 - how to iterate through a nested list element by element to create a different string each time 如何遍历数组并将每个元素传递给方法? - How to iterate through an array and pass each element onto a method? 如何正确遍历仅包含字符串的列表? - How to properly iterate through a list containing only strings? 如何通过 class 方法迭代元组列表中每个元组的第一个元素? - How can I iterate the first element of each tuple in a list of tuples through a class method? 如何仅提取列表中每个元素的一部分? - How to extract only a portion of each element in a list? 如何迭代并将列表中的每个元素与另一个元素相加以找到一个数字? - How to iterate and sum each element in a list with another to find a number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM