我最近开始使用python 2.7。 我有一些数据,我传递给亚马逊的产品API,使其成为批量调用,我希望每次调用传递10个值,因为这是每批次调用的最大ID或关键字。
这是一个问题,如何只向函数传递10个值。我总共有76个值(可能会增加),这是一个列表,最后是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
答案 0 :(得分:0)
我想你想做这样的事情:
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]