What is the way to separate range and pass the array to the function?
本问题已经有最佳答案,请猛点这里访问。
我有一组
如果我这样做
1 | len(attachmentList) |
结果为199930;我想每次发送999个元素到api请求(附件)函数
所以伪代码是这样的
1 2 3 4 5 6 | count=0 for (i=0,i<len(attachmentList),i++) count++ if count=999: api_request(attachmentList[i-999:i]) count=0 |
写循环的方法是什么,或者有另一种解决方法。
您可以以999为单位循环:
1 2 | for i in range(0, len(attachmentList), 999): api_request(attachmentList[i:i+999]) |
使用
1 2 3 4 5 | def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) |
然后:
1 2 | for chunk in grouper(attachmentList, 1000): api_request(chunk) |
您可以使用
1 2 3 4 | previous = 0 for i in range(999,len(attachmentList),999): api_request(attachmentList[previous:i] previous = i |