Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用前一个输出值作为输入循环函数_Python_Function_Loops - Fatal编程技术网

Python 使用前一个输出值作为输入循环函数

Python 使用前一个输出值作为输入循环函数,python,function,loops,Python,Function,Loops,我试图查询一个API,但它一次只提供100条记录,并提供一个分支记录,我需要使用它来查询下100条记录。我可以编写一个函数来查询我的结果,但是在循环我的函数以使用上一个函数的输出作为下一个函数的输入时遇到了问题。以下是我希望我的循环基本上要做的: def query(my_offset=None): page = at.get('Accounts',offset=my_offset) a = page['records'] return str(page['offset'

我试图查询一个API,但它一次只提供100条记录,并提供一个分支记录,我需要使用它来查询下100条记录。我可以编写一个函数来查询我的结果,但是在循环我的函数以使用上一个函数的输出作为下一个函数的输入时遇到了问题。以下是我希望我的循环基本上要做的:

def query(my_offset=None):
    page = at.get('Accounts',offset=my_offset)
    a = page['records']
    return str(page['offset'])

query()
query(query())
query(query(query(query())))
query(query(query(query(query()))))
query(query(query(query(query(query())))))
...
...
...
...

我猜
res
可以有一个特殊的值,指示没有返回更多的行,如果是这样,可以部署while循环:

res = query()
while True:
    res = query(res)
    if not res: break

您只需将查询结果重新绑定到
res
,并在每次迭代中重复使用它

我猜
res
可以有一个特殊值,指示没有返回更多行,如果是这样,可以部署while循环:

res = query()
while True:
    res = query(res)
    if not res: break

您只需将查询结果重新绑定到
res
,并在每次迭代中重复使用它

尝试从外部收集结果,然后再次调用该函数:

results = []
MAX_ITERATIONS = 20
offset = None

def query(offset=None):
  page = at.get('Accounts', offset=offset)
  return page['records'], page['offset']

while len(results) <= MAX_ITERATIONS:
   result, offset = query(offset)
   results.append(result)
results=[]
最大迭代次数=20
偏移量=无
def查询(偏移量=无):
page=at.get('Accounts',offset=offset)
返回页面['records'],页面['offset']

而len(results)尝试从外部收集结果,然后再次调用该函数:

results = []
MAX_ITERATIONS = 20
offset = None

def query(offset=None):
  page = at.get('Accounts', offset=offset)
  return page['records'], page['offset']

while len(results) <= MAX_ITERATIONS:
   result, offset = query(offset)
   results.append(result)
results=[]
最大迭代次数=20
偏移量=无
def查询(偏移量=无):
page=at.get('Accounts',offset=offset)
返回页面['records'],页面['offset']

而len(results)您如何返回最终结果?考虑:

def query():
    offset = None
    a = []
    while True:
        page = at.get('Accounts',offset=offset)
        a.extend(page['records'])
        offset = page['offset']
        if not offset: break
    return a

这实际上只是Jim在收集和返回页面结果时的回答

您如何返回最终结果?考虑:

def query():
    offset = None
    a = []
    while True:
        page = at.get('Accounts',offset=offset)
        a.extend(page['records'])
        offset = page['offset']
        if not offset: break
    return a
def query(result):
    # perform query
    return result

def end_query_condition(result):
    # if want to continue query:
    return True
    # if you want to stop:
    return False

continue_query = True
result = None
while continue_query:
    result = query(result)
    continue_query = end_query_condition(result)

这实际上只是Jim在收集和返回页面结果时的回答

您可以简单地使函数递归,而不需要任何循环,如下所示:

def query(result):
    # perform query
    return result

def end_query_condition(result):
    # if want to continue query:
    return True
    # if you want to stop:
    return False

continue_query = True
result = None
while continue_query:
    result = query(result)
    continue_query = end_query_condition(result)
def query(my_offset=None):
    if not offset:
        return None
    page = at.get('Accounts',offset=my_offset)
    a = page['records']
    return query(str(page['offset']))

您可以简单地使函数递归,而不需要任何循环,如下所示:

def query(my_offset=None):
    if not offset:
        return None
    page = at.get('Accounts',offset=my_offset)
    a = page['records']
    return query(str(page['offset']))

您希望何时终止?我希望在没有更多偏移记录时终止它,基本上是在没有返回任何内容时。您希望何时终止?我希望在没有更多偏移记录时终止它,基本上是在没有返回任何内容时终止它。