Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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_Arrays_Algorithm_Function_Loops - Fatal编程技术网

Python 通过两个函数运行循环,将彼此的输出作为输入

Python 通过两个函数运行循环,将彼此的输出作为输入,python,arrays,algorithm,function,loops,Python,Arrays,Algorithm,Function,Loops,比如说,我有两个功能 def f1(arr,k): #does something here with elements of array arr starting from index k and returns i.. #EDIT: we don't need value1 from f1, i=k while arr[i]==0: i=i+1 return i 还有一个功能 def f2(arr,arr2,k): # doe

比如说,我有两个功能

def f1(arr,k):
    #does something here with elements of array arr starting from index k and returns i..
    #EDIT: we don't need value1 from f1, 
    i=k
    while arr[i]==0:
        i=i+1
    return i
还有一个功能

def f2(arr,arr2,k):
   # does something here with elements of array arr starting from index k (k is output from function f1)...
    #EDIT: following is the code for f2:
    pr=0
    if arr[k]==1:
        i=k
        while (pr<10 and pr>-10) and (arr2[i+1]!=2):
            pr=pr+(arr2[i+1]-arr2[i])
        i=i+1

    if arr[k]==2:
        i=k
        while (pr<10 and pr>-10) and (arr[i+1]!=1):
            pr=pr+(arr2[i]-arr2[i+1])
            i=i+1
    return i+1, pr
有人能提供一些指导吗

编辑:在完成@AlexForGill回答的操作之后,我得到了函数f2的索引越界错误
def最终版本(arr,arr2):
x=0
plist=[]
当x=len(arr)-1:#用于应用第二个函数的保护子句
打破
x、 值2=f2(arr,arr2,x)
plist.append(值2)
返回层

以下内容对您有用吗

def final(arr):
    x = 0
    accumulator = []
    while (x < len(arr)):
        x, value1 = f1(arr, x)
        if (x >= len(arr)): # guard clause for applying second function
            break
        x, value2 = f2(arr, x)
        accumulator.append(value2)
    return accumulator
def最终版本(arr):
x=0
累加器=[]
而(x=len(arr)):#用于应用第二个函数的保护子句
打破
x、 值2=f2(arr,x)
累加器。追加(值2)
回流蓄能器
while
循环在调用
f1
f2
之间交替进行。
f1
的结果是更新的索引,它被分配给
x
。然后将该值传递到
f2
,该值还返回更新后的索引,该索引再次分配给
x


value2
然后被附加到循环结束时返回的累加器列表中。

您能提供一个简短的解释吗?很好,但我认为这也应该在列表中收集
value2
。@tobias_k你说得对-我在question@AmanArora我添加了一个解释和一个列表累加器,用于收集所有
value2
sThank you。但是当函数f1在列表末尾附近找不到索引时,我得到一个IndexOutOfBoundsException。(根据函数中描述的一些条件)那么您根本不需要
value1
?显然,我意识到我不需要value1。我建议您将
f1
放入
f2
,如果
f1
与其余部分无关,则循环f2。
def final(arr,arr2):
x=0
plist=[]
while x < len(arr)-1:
    x = f1(arr, x)
    if x >= len(arr)-1: # guard clause for applying second function
        break
    x, value2 = f2(arr,arr2, x)
    plist.append(value2)
return plist
def final(arr):
    x = 0
    accumulator = []
    while (x < len(arr)):
        x, value1 = f1(arr, x)
        if (x >= len(arr)): # guard clause for applying second function
            break
        x, value2 = f2(arr, x)
        accumulator.append(value2)
    return accumulator