在 python 中的 'n' 值使用 for-loop 和 step-loop 在列表中添加 'n' 值

Adding 'n' values in list using for-loop and step-loop for that 'n' values in python

我最近开始使用 python 2.7。 我有一些数据要传递给亚马逊的产品 API,以使其成为批量调用我想每次调用传递 10 个值,因为这是每个批量调用的最大 ID 或关键字。

这里有个问题,如何只传递10个值给函数。我总共有大约 76 个值(它可能会增加),这是一个列表,end.I 中的 6 个可以使用 *args 从列表中读取值但是只获得 10 个值我如何使用 for-loop 语句处理它或任何循环。

我想做这样的事情

data = rows_db
count = 76

for id in data[range start , count ]:
    ids = id #copy 10 values or less 
    foo(ids)
    start = start + 10 

def foo(*ids):
    #process and retrieve values

我猜你想做这样的事情:

data_copy = list(data)  # you can replace any appearance of data_copy with data if you don't care if it is changed
while data_copy:  # this is equivalent to: while len(data_copy) != 0:
    to = min(10, len(data_copy))  # If there are less then 10 entries left, the length will be smaller than ten, so that it is either 10 or the (smaller) length. This is the amount of data that's processed
    f(data_copy[:to])  # make the function call with any value up to 'to'
    del data_copy[:to]  # delete the data, because we already processed it

这个:

def f(x): print(x)
data = list(range(53))  # list from 0 (included) to 52 (included)
# here is the top part

产生

的预期输出
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52]